-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchronos.py
102 lines (80 loc) · 3.34 KB
/
chronos.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
# python3.6
# ----------------------------------------------------------------------------------
#
# `_` `_,_` _' `,`
# -#@@- >O#@@@@u B@@> 8@E
# :)ilc}` `=|}uccccVu}r" VQz `@@#Mhzk= |8M `=v}ucccccuY), `~v}uVVcccccV#@$
# ^Q@#EMqK.I#@QRdMqqMdRQ@@Q, Q@B `@@BqqqW^ W@@` e@@QRdMMMMbEQ@@8: i#@BOMqqqqqqqM#@$
# D@@` )@@x <@@T Q@B `@@q W@@`>@@l :@@z`#@d Q@$
# D@# ?@@@##########@@@} Q@B `@@q W@@`^@@@##########@@@y`#@W Q@$
# 0@# )@@d!::::::::::::` Q@B `@@M W@@`<@@E!::::::::::::``#@b `B@$
# D@# `m@@#bGPP} Q@B `@@q W@@` 3@@BbPPPV y@@QZPPPPPGME#@8=
# *yx .*icywwv )yv }y> ~yT .^icywyL .*]uywwwwycL^-
#
# (c) 2021 Reified Ltd. W: www.reified.co.uk E: [email protected]
#
# ----------------------------------------------------------------------------------
#
# Classes for chronology-related concepts. A Stopwatch and a CountdownTimer.
#
# ----------------------------------------------------------------------------------
import time
####################################################################################
#
# class Stopwatch
#
####################################################################################
class Stopwatch:
def __init__(self):
self._start = time.time()
self._stop = self._start
self._accumulative = 0.0
self._running = False;
def start(self):
self._start = time.time()
self._running = True
def stop(self):
self._stop = time.time()
elapsed = self._stop - self._start
self._accumulative += elapsed
self._start = self._stop
self._running = False;
def elapsed(self):
end = time.time() if self._running else self._stop
elapsed = end - self._start
return elapsed + self._accumulative
def restart(self):
self._start = time.time()
self._stop = self._start
self._accumulative = 0.0
self._running = True;
def reset(self):
self._start = time.time()
self._stop = self._start
self._accumulative = 0.0
self._running = False;
def _now():
return time.time()
####################################################################################
#
# class CountdownTimer
#
####################################################################################
class CountdownTimer:
def __init__(self, interval):
self._stopwatch = Stopwatch()
self._period = interval
def start(self):
self._stopwatch.start()
def stop(self):
self._stopwatch.stop()
def remaining(self):
timeSpent = self._stopwatch.elapsed()
timeLeft = self._period - timeSpent
return timeLeft if (timeLeft > 0.0) else 0.0
def hasExpired(self):
return self._stopwatch.elapsed() >= self._period
def period(self, interval):
self._period = interval
def restart(self):
self._stopwatch.restart()