-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_class_timer.py
127 lines (92 loc) · 3.55 KB
/
py_class_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
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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 22 16:23:55 2018
@author: kylek
Real quick timer object for duration calculation.
If you want a singular timer object that can be started and stopped (paused) linearly, just use one timer object. If you need
multiple timers that have differing start/stops (overlapping), create multiple objects, like:
timer_A = Timer()
timer_B = Timer()
You can reset the timer object with object_name.reset(). The last sum of timer intervals will be stored in object_name.last_timer for future referencing.
"""
import time
class Timer(object):
"""
Simple timer object that allows you to start and stop a timer counter. Uses
the time.time() unix time value to create timer intervals.
Basic Usage:
object_name.start() --> Starts a timer interval.
object_name.stop() --> Stops a timer interval, stores value.
object_name.timer() --> Returns the current sum of all timers.
"""
stored_time = []
last_timer = None
start_time = 0
active = 0
def __init__(self):
pass
def __repr__(self):
""" Returns the value of timer() """
return str(self.timer())
def start(self):
""" Stores the current start time. Sets timer to active. """
if not self.active:
self.active = 1
self.start_time = time.time()
def stop(self):
""" Stores current time from start_time in stored_time. """
if self.active:
self.stored_time.append(time.time() - self.start_time)
self.active = 0
self.start_time = 0
def reset(self):
""" Blanks the stored_time list, sets start_time and active to 0. """
if self.active:
self.stop()
self.last_timer = sum(self.stored_time)
self.stored_time = []
self.active = 0
self.start_time = 0
def interval(self):
""" Returns the current active timer interval value. """
if self.active:
return time.time() - self.start_time
else:
return "No currently running timer."
def timer(self):
""" Returns the sum of the entire timeset, including any currently running interval. """
if self.active:
curr_timer = time.time() - self.start_time
else:
curr_timer = 0
return curr_timer + sum(self.stored_time)
if __name__ == "__main__":
times = Timer()
print('Start Timer')
times.start()
time.sleep(5) # Sleep for 5 seconds
times.stop()
print('Timer Stopped: Sum of all times: {}'.format(times))
print('Start Timer')
times.start()
time.sleep(3)
times.stop()
print('Stop Timer')
print('Timer Stopped: Sum of all times: {}'.format(times))
print('Start Timer')
times.start()
time.sleep(3)
print('Current active interval: {}'.format(times.interval()))
time.sleep(2)
print('Current active interval: {}'.format(times.interval()))
time.sleep(2)
times.stop()
print('Stop Timer')
print('Sum of ALL times: {}'.format(times.timer()))
print('Should be approximately 5s + 3s + 7s = 15s')
# Timer ---
# .start --> Starts a timer (stores the current unix time)
# .stop --> Stops a timer (takes current time, subtracts the stored time, and puts it into a list element)
# .reset --> Blanks the STORED TIME list elements.
# .time --> Gets the current timer value.
# print() .. same as .time