-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
46 lines (40 loc) · 1.26 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Miscellaneous utility functions
"""
import sys, time, signal
def format_size(bytes, pretty = False):
"""
Format an int size (in bytes) into a string.
Optionally, an eye-friendly formatting can be used
"""
if pretty:
if bytes < 1024:
return str(bytes)
elif bytes < 1024 * 1024:
return '%.1fK' % (bytes / 1024.0)
elif bytes < 1024 * 1024 * 1024:
return '%.1fM' % (bytes / (1024 * 1024.0))
else:
return '%.1fG' % (bytes / (1024 * 1024 * 1024.0))
else:
return str(bytes)
def format_time(seconds, pretty = False):
"""
Format a time value (given in seconds from the Epoch) into a string.
If the pretty flag is set, a human-readable format is used
"""
if pretty:
if time.localtime(seconds).tm_year == time.localtime().tm_year:
format = '%b %d %H:%M'
else:
format = '%Y %b %d'
return time.strftime(format, time.localtime(seconds))
else:
return str(int(seconds))
def exit_on_ctrl_c(signum, frame):
"""
Signal handler that catches SIGINT and terminates the program
"""
if signum == signal.SIGINT:
print('^C')
sys.exit(1)