Skip to content

Commit e9a0f80

Browse files
committed
Prettier formatting
1 parent 43ced73 commit e9a0f80

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

autotime.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,36 @@
44

55

66
class LineWatcher(object):
7-
def __init__(self, ip):
8-
self.shell = ip
7+
def __init__(self):
98
self.start_time = 0.0
109

1110
def start(self):
1211
self.start_time = time.time()
1312

1413
def stop(self):
15-
print('time: %.6f s' % (time.time() - self.start_time))
14+
diff = time.time() - self.start_time
15+
assert diff > 0
16+
print('time: %s' % format_delta(diff))
17+
18+
19+
def format_delta(num, suffix='s', threshold=1000):
20+
base_unit = 1000000000
21+
num *= base_unit # smallest unit is 1 / 1000000000 of the biggest unit
22+
for unit in ['n', 'u', 'm', '']:
23+
if num < threshold:
24+
return '%3.2f %s%s' % (num, unit, suffix)
25+
num /= threshold
26+
return '%.2f %s' % (num, suffix)
27+
28+
29+
timer = LineWatcher()
1630

1731

1832
def load_ipython_extension(ip):
19-
timer = LineWatcher(ip)
2033
ip.events.register('pre_run_cell', timer.start)
2134
ip.events.register('post_run_cell', timer.stop)
2235

2336

2437
def unload_ipython_extension(ip):
25-
timer = LineWatcher(ip)
2638
ip.events.unregister('pre_run_cell', timer.start)
2739
ip.events.unregister('post_run_cell', timer.stop)

0 commit comments

Comments
 (0)