-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
100 lines (77 loc) · 2.88 KB
/
timer.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
from time import time;
from math import floor;
# Supporting class to collect timing information.
class Timer:
# A constant for state management indicating that the timer is stopped.
STOPPED = 0;
# A constant for state management indicating that the timer is running.
RUNNING = 1;
# Initializes the timer to a specific time delta, that defaults to zero.
# The timer is left in the stopped state until manually started.
def __init__(self, delta_t = 0):
self.state = Timer.STOPPED;
self.delta_t = delta_t;
# Starts the timer if it is currently stopped. Returns the timer itself.
def start(self):
if self.state == Timer.STOPPED:
self.state = Timer.RUNNING;
self.t = time();
return self;
# Stops the timer if it is currently running. Returns the timer itself.
def stop(self):
if self.state == Timer.RUNNING:
self.state = Timer.STOPPED;
self.delta_t += time() - self.t;
return self;
# Stops the timer and re-initializes it to a specific time delta, that
# defaults to zero. Returns the timer itself.
def reset(self, delta_t = 0):
self.state = Timer.STOPPED;
self.delta_t = delta_t;
return self;
# Resets the timer and then starts it again. Returns the timer itself.
def restart(self):
self.reset();
self.start();
return self;
# Peeks at the timer, returning the number of seconds elapsed. If the timer is
# stopped, the time delta is returned. Otherwise, the sum of the time delta
# and the current offset of the running timer is returned.
def peek(self):
tmp_delta_t = self.delta_t;
if self.state == Timer.RUNNING:
tmp_delta_t += time() - self.t;
return tmp_delta_t;
# Adds the time deltas of two stopped timers, returning a new timer with said
# time delta. The timer is left in the stopped state until manually started.
# If either timer is running, an exception is raised.
def __add__(a, b):
if (Timer.STOPPED != a.state) or (Timer.STOPPED != b.state):
raise Exception("Error: Cannot add running timers.");
return Timer(a.delta_t + b.delta_t);
# Represents the timer as a string.
def __repr__(self):
# Get a temporary time delta.
tmp_delta_t = self.peek();
# Compute hours, minutes, seconds, milliseconds and microseconds.
hours = floor(tmp_delta_t / 3600);
mins = floor(tmp_delta_t / 60) % 60;
secs = floor(tmp_delta_t) % 60;
ms = int(floor((10 ** 3) * tmp_delta_t)) % (10 ** 3);
us = int(floor((10 ** 6) * tmp_delta_t)) % (10 ** 3);
# Format as a human-readable string.
hr = "";
if tmp_delta_t >= 3600:
hr += str(hours) + " hour";
if hours > 1:
hr += "s ";
else:
hr += " ";
if tmp_delta_t >= 60:
hr += str(mins) + " min ";
if tmp_delta_t >= 1:
hr += str(secs) + " sec ";
if tmp_delta_t >= 10 ** -3:
hr += str(ms) + " ms ";
hr += str(us) + " µs";
return hr;