-
Notifications
You must be signed in to change notification settings - Fork 2
/
stopwatch.py
142 lines (118 loc) · 4.59 KB
/
stopwatch.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
''' written by Richard Pattis @rpattis.uci.edu
used for testing by Klint Segarra
'''
import time
class Stopwatch:
"""
Models a stopwatch, tracking wallclock time.
Stopwatches can be started (run forwards and backwards), stopped, and read.
"""
def __init__(self, running_now=False, running_forward=True, elapsed_prior=0, last_start_time=0):
"""
Instantiate like Stopwatch()
Stopwatch instance is stopped with 0 elapsed wallclock time.
"""
self._running_now = running_now
self._running_forward = running_forward
self._elapsed_prior = elapsed_prior
self._last_start_time = (time.clock() if running_now else last_start_time)
def reset(self):
"""
Stopwatch is now stopped with 0 elapsed wallclock time.
"""
self._running_now = False
self._running_forward = True
self._elapsed_prior = 0
self._last_start_time = 0
def start(self):
"""
Stopwatch is now running forwards, accumulating wallclock time.
"""
if self._running_now:
if self._running_forward:
return
else: # running backward
self._update() # update, then start running forward
self._last_start_time = time.clock()
self._running_now = True
self._running_forward = True
def start_backwards(self):
"""
Stopwatch is now running backwards, [un]accumulating wallclock time.
"""
if self._running_now:
if not self._running_forward:
return
else: # running forward
self._update() # update, then start running backward
self._last_start_time = time.clock()
self._running_now = True
self._running_forward = False
def stop(self):
"""
Stopwatch is now stopped: not accumulating wallclock time.
"""
if not self._running_now:
return
self._running_now = False
self._update()
def read(self):
"""
Returns the elapsed wallclock time in seconds as a float.
Works when the Stopwatch is running and when it is stopped.
"""
if self._running_now:
self._update()
return self._elapsed_prior
def __str__(self):
"""
Returns a string representation for a Stopwatch, such that
s2 = eval(str(s1)) copies the state of s1 at the time of the call
"""
return "Stopwatch("+ \
str(self._running_now) + "," + \
str(self._running_forward) + "," + \
str(self._elapsed_prior) + "," + \
str(self._last_start_time) + ")"
def status(self):
"""
Returns the status of the Stopwatch: a tuple
(running?, forward?, elapsed) : (bool, bool, float)
"""
return (self._running_now, self._running_forward, self.read())
def _update(self):
"""
Accumulates time from last start time; now becomes last start time
"""
self._elapsed_prior += (1 if self._running_forward else -1) * (time.clock() - self._last_start_time)
self._last_start_time = time.clock()
if __name__ == "__main__":
import prompt
print("Begin testing Stopwatch")
commandPrompt = \
"""\nTesting Stopwatch:
Commands Queries Other
> - start ? - read . - exec(...)
s - stop : - status q - quit
< - start_backwards _ - __str__
r - reset\nCommand"""
s = Stopwatch()
while True:
action = prompt.for_char(commandPrompt, legal=">s<r?:_.q")
try:
if action == ">": s.start()
elif action == "s": s.stop()
elif action == "<": s.start_backwards()
elif action == "r": s.reset()
elif action == "?": print(" Elapsed =", s.read(), "seconds")
elif action == ":": print(" Status =", s.status())
elif action == "_": print(" __str__ =", str(s))
elif action == ".": exec(prompt.for_string(" Enter command to exec (instance=s)"))
elif action == "q": break
else: print(" Unknown command")
except AssertionError as report:
print(" AssertionError exception caught:", report)
except Exception as report:
import traceback
traceback.print_exc()
print("\nFinished testing Stopwatch")