-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (82 loc) · 2.93 KB
/
main.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
from time import time
# Handle the running state of the timer
RUNNING = 1
PAUSED = 0
STOPPED = -1
def get_current_time():
return time()
class Timer:
def __init__(self):
# Maintains the state of the timer (RUNNING, PAUSED, STOPPED)
self.state = STOPPED
# Time that self.start() was called
self._start_time = 0
# Time that self.end() was called
self._end_time = 0
# Time spent in pause
self._pause_time = 0
# Total time spent in pause
self._total_pause_time = 0
# All laps
self._laps = {}
# Total lap count, inclusive of the current lap
self._lap_count = -1
def start(self, name="start", lap=True):
"""Starts the timer"""
self.state = RUNNING
self._start_time = get_current_time()
# Create a lap with this start time
if lap:
self.lap(name)
def end(self):
"""Ends the timer"""
self.state = STOPPED
self._end_time = get_current_time()
self._lap_count += 1
self._end_lap()
def lap(self, name=None):
"""Ends the current lap and creates a new lap in lap object"""
if self._start_time == 0:
self.start(lap=False)
self._lap_count += 1
self._end_lap()
lap = {
"name": name if name else self._lap_count,
"start": get_current_time(),
"end": -1,
"total": -1,
}
self._laps[self._lap_count] = lap
def _end_lap(self):
"""Assign end and total times to the previous lap"""
if len(self._laps) > 0:
self._laps[self._lap_count - 1]["end"] = get_current_time()
self._laps[self._lap_count - 1]["total"] = (
self._laps[self._lap_count - 1]["end"]
- self._laps[self._lap_count - 1]["start"]
)
def summary(self):
"""Returns a summary of all timer activity so far"""
return {
"running": self.state,
"start": self._start_time,
"end": self._end_time,
"total": self._end_time - self._start_time,
"paused": self._total_pause_time,
"laps": self._laps,
}
def pause(self):
"""Initiates a pause in the timer"""
self.state = PAUSED
self._pause_time = get_current_time()
def unpause(self):
"""Cancels the pause previously set"""
if self.state == PAUSED:
self.state = RUNNING
self._total_pause_time = self._total_pause_time + (
get_current_time() - self._pause_time
)
self._pause_time = 0
def pretty_summary(self, json):
"""an unrelated pretty print helper, yes you have to pass in THE json package"""
return json.dumps(self.summary(), indent=2)