-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswpy.py
121 lines (89 loc) · 3.48 KB
/
swpy.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
swpy
====
A simple, yet useful stopwatch library.
Usage
-----
Use context manager or initializing with class, it depends on you.
Example:
>>> from swpy import Timer
>>> import time
# the way of using context manager
>>> with Timer():
... time.sleep(1)
[timer-1557406243.3309178] started.
[timer-1557406243.3309178] finish time: 1.00 sec.
# the way of using initializing class
>>> timer = Timer():
>>> time.sleep(1)
>>> timer.stop()
[timer-1557406243.3309178] started.
[timer-1557406243.3309178] finish time: 1.00 sec.
"""
import logging
import time
__author__ = 'Peinan ZHANG'
__version__ = (0, 1, 2)
__license__ = 'MIT'
class Timer:
def __init__(self, name=None, logger=None, level=logging.DEBUG, digits=2, callback=None):
"""
Args:
:param name str: The name of the timer. default: 'time-{NOW}'
:param logger logger: Specific the logger. default: print
:param level: The log level while using logger.
:param digits int: The digit of time demical. digits=2 will look like 1.00.
:param callback callable: The callback.
Example:
>>> with Timer():
... time.sleep(1)
[timer-1557406243.3309178] started.
[timer-1557406243.3309178] finish time: 1.00 sec.
"""
self.timername = name if name else f'timer-{time.time()}'
self.print = print if not logger else lambda msg: logger.log(level, msg)
self.level = level
self.digits = digits
self.callback = callback
self.started_at = time.time()
self.times = [self.started_at]
self.stopped_at = None
self.elapsed = None
self.print(f'[{self.timername}] started.')
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.stopped_at = time.time()
self.times.append(self.stopped_at)
self.elapsed = self.stopped_at - self.started_at
msg = f'[{self.timername}] finish time: {self.elapsed:.{self.digits}f} sec.'
self.print(msg)
if self.callback: self.callback(msg)
def start(self, title='', callback=None):
self.started_at = time.time()
self.times[0] = self.started_at
title_block = f'[{title}] ' if title else ''
msg = f'[{self.timername}] {title_block}started.'
self.print(msg)
if callback: callback(msg)
def stop(self, title='', callback=None):
self.stopped_at = time.time()
self.times.append(self.stopped_at)
self.elapsed = self.stopped_at - self.started_at
title_block = f'[{title}] ' if title else ''
msg = f'[{self.timername}] {title_block}finish time: {self.elapsed:.{self.digits}f} sec.'
self.print(msg)
if callback: callback(msg)
elif self.callback: self.callback(msg)
def split(self, title='', callback=None):
self.times.append(time.time())
title_block = f'[{title}] ' if title else ''
msg = f'[{self.timername}] {title_block}split time: {self.times[-1] - self.times[0]:.{self.digits}f} sec.'
self.print(msg)
if callback: callback(msg)
def lap(self, title='', callback=None):
self.times.append(time.time())
title_block = f'[{title}] ' if title else ''
msg = f'[{self.timername}] {title_block}lap time: {self.times[-1] - self.times[-2]:.{self.digits}f} sec.'
self.print(msg)
if callback: callback(msg)