-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
47 lines (36 loc) · 1.05 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
47
import time
import traceback
import humanize
def human_size(x):
return humanize.naturalsize(x, binary=True)
def timed_print(f, **kwargs):
elapsed, x = timed_format(f, **kwargs)
print(elapsed)
return x
def timed_format(f, **kwargs):
elapsed_s, x = timed(f, **kwargs)
elapsed = '[%s]' % format_duration(elapsed_s)
return elapsed, x
def timed(f, if_error_return='exception'):
start_s = time.time()
try:
x = f()
except Exception as e:
traceback.print_exc()
x = e if if_error_return == 'exception' else if_error_return
elapsed_s = time.time() - start_s
return elapsed_s, x
def format_duration(secs):
if secs < 0:
return '-' + format_duration(-secs)
else:
s = int(secs) % 60
m = int(secs) // 60 % 60
h = int(secs) // 60 // 60
res = ':'.join('%02.0f' % x for x in (
[m, s] if h == 0 else [h, m, s]
))
if isinstance(secs, float):
ms = round(secs % 1, 3)
res += ('%.3f' % ms)[1:]
return res