oclock 1.4.2__tar.gz → 1.4.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oclock-1.4.3/.gitattributes +2 -0
- {oclock-1.4.2 → oclock-1.4.3}/PKG-INFO +3 -2
- {oclock-1.4.2 → oclock-1.4.3}/oclock/__init__.py +3 -4
- {oclock-1.4.2 → oclock-1.4.3}/oclock/__main__.py +23 -10
- {oclock-1.4.2 → oclock-1.4.3}/oclock/countdown.py +19 -16
- {oclock-1.4.2 → oclock-1.4.3}/oclock/event.py +10 -32
- {oclock-1.4.2 → oclock-1.4.3}/oclock/general.py +12 -13
- {oclock-1.4.2 → oclock-1.4.3}/oclock/loop.py +33 -24
- {oclock-1.4.2 → oclock-1.4.3}/oclock/performance.py +25 -16
- {oclock-1.4.2 → oclock-1.4.3}/oclock/timer.py +18 -16
- {oclock-1.4.2 → oclock-1.4.3}/oclock.egg-info/PKG-INFO +3 -2
- {oclock-1.4.2 → oclock-1.4.3}/oclock.egg-info/SOURCES.txt +1 -0
- {oclock-1.4.2 → oclock-1.4.3}/oclock.egg-info/top_level.txt +0 -1
- {oclock-1.4.2 → oclock-1.4.3}/.gitignore +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/AccuracyTests.md +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/Examples.ipynb +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/LICENSE +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/MANIFEST.in +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/README.md +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/example.py +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/countdown.gif +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_macos_1000ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_macos_100ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_macos_10ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_macos_1ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_macos_40ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_windows_1000ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_windows_100ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_windows_10ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_windows_1ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/precisetimer_windows_40ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/ribosome.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_interval_exceeded.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_macos_1000ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_macos_100ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_macos_10ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_macos_1ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_macos_40ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_windows_1000ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_windows_100ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_windows_10ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_windows_1ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/media/img/timer_windows_40ms.png +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/oclock.egg-info/dependency_links.txt +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/oclock.egg-info/requires.txt +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/pyproject.toml +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/setup.cfg +0 -0
- {oclock-1.4.2 → oclock-1.4.3}/tests/test_oclock.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: oclock
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.3
|
|
4
4
|
Summary: Tools for timed, no-drift loops of constant duration, and other misc. timing tools (GUI countdown, context managers etc.)
|
|
5
5
|
Author-email: Olivier Vincent <ovinc.py@gmail.com>
|
|
6
6
|
License: GNU GPLv3
|
|
@@ -15,6 +15,7 @@ Requires-Python: >=3.5
|
|
|
15
15
|
Description-Content-Type: text/markdown
|
|
16
16
|
License-File: LICENSE
|
|
17
17
|
Requires-Dist: importlib-metadata
|
|
18
|
+
Dynamic: license-file
|
|
18
19
|
|
|
19
20
|
# About
|
|
20
21
|
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
from .timer import Timer
|
|
24
23
|
from .countdown import Countdown
|
|
25
24
|
from .general import parse_time, measure_time, measure_duration, after
|
|
@@ -29,6 +28,6 @@ from .event import Event
|
|
|
29
28
|
# from importlib.metadata import version (only for python 3.8+)
|
|
30
29
|
from importlib_metadata import version
|
|
31
30
|
|
|
32
|
-
__version__ = version(
|
|
33
|
-
__author__ =
|
|
34
|
-
__license__ =
|
|
31
|
+
__version__ = version("oclock")
|
|
32
|
+
__author__ = "Olivier Vincent"
|
|
33
|
+
__license__ = "GNU GPLv3"
|
|
@@ -19,21 +19,34 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
import argparse
|
|
24
23
|
|
|
25
24
|
from .countdown import Countdown
|
|
26
25
|
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
description = (
|
|
28
|
+
"GUI countdown clock based on the oclock module.\n\n"
|
|
29
|
+
"Examples\n"
|
|
30
|
+
"--------\n"
|
|
31
|
+
"Timer for 5 seconds:\n"
|
|
32
|
+
">>> python -m oclock ::5\n\n"
|
|
33
|
+
"Timer for 1 hour and 25 minutes:\n"
|
|
34
|
+
">>> python -m oclock 1:25:\n"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
parser = argparse.ArgumentParser(
|
|
38
|
+
description=description,
|
|
39
|
+
formatter_class=argparse.RawTextHelpFormatter,
|
|
40
|
+
prog='python -m oclock',
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
msg = (
|
|
44
|
+
"Duration in hh:mm:ss format, e.g. "
|
|
45
|
+
"1:25:30 (1 hour, 25 min and 30 seconds), "
|
|
46
|
+
"or ::5 (5 seconds)."
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
parser.add_argument("time", type=str, help=msg)
|
|
37
50
|
|
|
38
51
|
args = parser.parse_args()
|
|
39
52
|
countdown = Countdown(args.time)
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
import tkinter as tk
|
|
24
23
|
from datetime import timedelta
|
|
25
24
|
|
|
@@ -33,10 +32,10 @@ from .general import parse_time
|
|
|
33
32
|
# ========================== Appearance parameters ===========================
|
|
34
33
|
|
|
35
34
|
|
|
36
|
-
bgcolor =
|
|
37
|
-
textcolor =
|
|
38
|
-
donecolor =
|
|
39
|
-
overduecolor =
|
|
35
|
+
bgcolor = "#0b3c5d"
|
|
36
|
+
textcolor = "#f7f7f7"
|
|
37
|
+
donecolor = "#64d253"
|
|
38
|
+
overduecolor = "#ff4c4c"
|
|
40
39
|
|
|
41
40
|
|
|
42
41
|
# =========================== Basic clock function ===========================
|
|
@@ -67,8 +66,8 @@ class Countdown:
|
|
|
67
66
|
(e.g. ::5 for 5 seconds, or 1:30: for 1.5 hours)
|
|
68
67
|
see oclock.parse_time() for details.
|
|
69
68
|
"""
|
|
70
|
-
self.total_time = parse_time(time_str)
|
|
71
|
-
self.timer = Timer(interval=0.2)
|
|
69
|
+
self.total_time = parse_time(time_str) # timedelta
|
|
70
|
+
self.timer = Timer(interval=0.2) # check remaining time every 0.2s
|
|
72
71
|
|
|
73
72
|
self.root = tk.Tk()
|
|
74
73
|
self.queue = Queue() # queue that gets the values of remaining time
|
|
@@ -82,7 +81,7 @@ class Countdown:
|
|
|
82
81
|
s = "{}, duration {}".format(self.__class__, str(self.total_time))
|
|
83
82
|
return s
|
|
84
83
|
|
|
85
|
-
# =============================== GUI Methods ================================
|
|
84
|
+
# =============================== GUI Methods ================================
|
|
86
85
|
|
|
87
86
|
def gui(self):
|
|
88
87
|
"""Set up and start GUI."""
|
|
@@ -91,8 +90,13 @@ class Countdown:
|
|
|
91
90
|
self.root.minsize(170, 40)
|
|
92
91
|
self.root.config(bg=bgcolor)
|
|
93
92
|
|
|
94
|
-
self.display = tk.Label(
|
|
95
|
-
|
|
93
|
+
self.display = tk.Label(
|
|
94
|
+
self.root,
|
|
95
|
+
font=("Helvetica", 30),
|
|
96
|
+
bg=bgcolor,
|
|
97
|
+
fg=textcolor,
|
|
98
|
+
text=str(self.total_time),
|
|
99
|
+
)
|
|
96
100
|
|
|
97
101
|
self.display.pack(expand=True)
|
|
98
102
|
|
|
@@ -109,19 +113,18 @@ class Countdown:
|
|
|
109
113
|
t_remaining = self.queue.get()
|
|
110
114
|
|
|
111
115
|
if t_remaining is not None:
|
|
112
|
-
|
|
113
116
|
if t_remaining.total_seconds() > 0: # ----- timer still counting down
|
|
114
117
|
self.display.config(text=str(t_remaining))
|
|
115
118
|
|
|
116
119
|
elif t_remaining.total_seconds() <= -5: # ------------- timer overdue
|
|
117
|
-
time_str =
|
|
120
|
+
time_str = "- " + str(-t_remaining)
|
|
118
121
|
self.display.config(text=time_str, fg=overduecolor)
|
|
119
122
|
|
|
120
123
|
else: # ------------------ timer just done (left on screen for 5 sec)
|
|
121
|
-
if not self.done:
|
|
122
|
-
print(
|
|
123
|
-
self.display.bell()
|
|
124
|
-
self.display.config(text=
|
|
124
|
+
if not self.done: # To print only once
|
|
125
|
+
print("Countdown Finished!")
|
|
126
|
+
self.display.bell() # Sound alert
|
|
127
|
+
self.display.config(text="Done!", fg=donecolor)
|
|
125
128
|
self.done = True
|
|
126
129
|
|
|
127
130
|
self.root.after(100, self.update) # update every 0.1 seconds
|
|
@@ -25,17 +25,13 @@ StackOverflow's CC BY-SA 4.0 license."""
|
|
|
25
25
|
# along with the oclock python package.
|
|
26
26
|
# If not, see <https://www.gnu.org/licenses/>
|
|
27
27
|
|
|
28
|
-
|
|
29
28
|
import time
|
|
30
29
|
import _thread
|
|
31
30
|
import datetime
|
|
32
31
|
|
|
33
32
|
|
|
34
33
|
class Event:
|
|
35
|
-
__slots__ = (
|
|
36
|
-
"_flag", "_lock", "_nl",
|
|
37
|
-
"_pc", "_waiters"
|
|
38
|
-
)
|
|
34
|
+
__slots__ = ("_flag", "_lock", "_nl", "_pc", "_waiters")
|
|
39
35
|
|
|
40
36
|
_lock_type = _thread.LockType
|
|
41
37
|
_timedelta = datetime.timedelta
|
|
@@ -74,10 +70,7 @@ class Event:
|
|
|
74
70
|
|
|
75
71
|
waiters.clear()
|
|
76
72
|
|
|
77
|
-
def wait(
|
|
78
|
-
self,
|
|
79
|
-
timeout: float = None
|
|
80
|
-
) -> bool:
|
|
73
|
+
def wait(self, timeout: float = None) -> bool:
|
|
81
74
|
with self._lock:
|
|
82
75
|
return self._wait(self._pc(), timeout)
|
|
83
76
|
|
|
@@ -96,7 +89,7 @@ class Event:
|
|
|
96
89
|
end: _timedelta = None,
|
|
97
90
|
waiter: _lock_type = None,
|
|
98
91
|
new_thread=_thread.start_new_thread,
|
|
99
|
-
thread_delay=_timedelta(milliseconds=3)
|
|
92
|
+
thread_delay=_timedelta(milliseconds=3),
|
|
100
93
|
) -> bool:
|
|
101
94
|
flag = self._flag
|
|
102
95
|
|
|
@@ -113,10 +106,7 @@ class Event:
|
|
|
113
106
|
if delay > thread_delay:
|
|
114
107
|
mark = end - thread_delay
|
|
115
108
|
waiter = self._new_waiter()
|
|
116
|
-
new_thread(
|
|
117
|
-
self._wait_thread,
|
|
118
|
-
(flag, mark, waiter)
|
|
119
|
-
)
|
|
109
|
+
new_thread(self._wait_thread, (flag, mark, waiter))
|
|
120
110
|
|
|
121
111
|
lock = self._lock
|
|
122
112
|
lock.release()
|
|
@@ -126,10 +116,7 @@ class Event:
|
|
|
126
116
|
waiter.acquire()
|
|
127
117
|
|
|
128
118
|
if end:
|
|
129
|
-
while (
|
|
130
|
-
not flag and
|
|
131
|
-
td(seconds=pc()) < end
|
|
132
|
-
):
|
|
119
|
+
while not flag and td(seconds=pc()) < end:
|
|
133
120
|
pass
|
|
134
121
|
|
|
135
122
|
finally:
|
|
@@ -147,7 +134,7 @@ class Event:
|
|
|
147
134
|
waiter: _lock_type,
|
|
148
135
|
td=_timedelta,
|
|
149
136
|
pc=_perf_counter,
|
|
150
|
-
sleep=time.sleep
|
|
137
|
+
sleep=time.sleep,
|
|
151
138
|
):
|
|
152
139
|
while not flag and td(seconds=pc()) < mark:
|
|
153
140
|
sleep(0.001)
|
|
@@ -167,28 +154,19 @@ class Event:
|
|
|
167
154
|
|
|
168
155
|
|
|
169
156
|
if __name__ == "__main__":
|
|
157
|
+
|
|
170
158
|
def test_wait_time():
|
|
171
159
|
wait_time = datetime.timedelta(microseconds=1)
|
|
172
160
|
wait_time = wait_time.total_seconds()
|
|
173
161
|
|
|
174
|
-
def test(
|
|
175
|
-
event=Event(),
|
|
176
|
-
delay=wait_time,
|
|
177
|
-
pc=time.perf_counter
|
|
178
|
-
):
|
|
162
|
+
def test(event=Event(), delay=wait_time, pc=time.perf_counter):
|
|
179
163
|
pc1 = pc()
|
|
180
164
|
event.wait(delay)
|
|
181
165
|
pc2 = pc()
|
|
182
|
-
pc1, pc2 = [
|
|
183
|
-
int(nbr * 1000000000)
|
|
184
|
-
for nbr in (pc1, pc2)
|
|
185
|
-
]
|
|
166
|
+
pc1, pc2 = [int(nbr * 1000000000) for nbr in (pc1, pc2)]
|
|
186
167
|
return pc2 - pc1
|
|
187
168
|
|
|
188
|
-
lst = [
|
|
189
|
-
f"{i}.\t\t{test()}"
|
|
190
|
-
for i in range(1, 11)
|
|
191
|
-
]
|
|
169
|
+
lst = [f"{i}.\t\t{test()}" for i in range(1, 11)]
|
|
192
170
|
print("\n".join(lst))
|
|
193
171
|
|
|
194
172
|
test_wait_time()
|
|
@@ -19,26 +19,25 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
import time
|
|
24
23
|
from datetime import timedelta
|
|
25
24
|
from contextlib import contextmanager
|
|
26
25
|
from threading import Thread
|
|
27
26
|
|
|
28
27
|
|
|
29
|
-
def _convert_str(s, kind=
|
|
28
|
+
def _convert_str(s, kind=""):
|
|
30
29
|
"""Convert individual str of hours, minutes or seconds to float.
|
|
31
30
|
|
|
32
31
|
Used by parse_time
|
|
33
32
|
kind is only for documenting the error message;
|
|
34
33
|
should be 'hours', 'minutes' or 'seconds'.
|
|
35
34
|
"""
|
|
36
|
-
if s ==
|
|
35
|
+
if s == "": # no value indicated, interpreted as zero
|
|
37
36
|
return 0
|
|
38
37
|
try:
|
|
39
38
|
val = float(s)
|
|
40
39
|
except ValueError:
|
|
41
|
-
raise ValueError(f
|
|
40
|
+
raise ValueError(f"{s} not a valid value for {kind}.")
|
|
42
41
|
else:
|
|
43
42
|
return val
|
|
44
43
|
|
|
@@ -87,13 +86,13 @@ def parse_time(time_str):
|
|
|
87
86
|
datetime.timedelta(microseconds=40000)
|
|
88
87
|
"""
|
|
89
88
|
try:
|
|
90
|
-
hours_str, minutes_str, seconds_str = time_str.split(
|
|
89
|
+
hours_str, minutes_str, seconds_str = time_str.split(":")
|
|
91
90
|
except ValueError:
|
|
92
|
-
raise ValueError(f
|
|
91
|
+
raise ValueError(f"{time_str} no a valid format of the form h:m:s")
|
|
93
92
|
|
|
94
|
-
hours = _convert_str(hours_str, kind=
|
|
95
|
-
minutes = _convert_str(minutes_str, kind=
|
|
96
|
-
seconds = _convert_str(seconds_str, kind=
|
|
93
|
+
hours = _convert_str(hours_str, kind="hours")
|
|
94
|
+
minutes = _convert_str(minutes_str, kind="minutes")
|
|
95
|
+
seconds = _convert_str(seconds_str, kind="seconds")
|
|
97
96
|
|
|
98
97
|
return timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
99
98
|
|
|
@@ -137,8 +136,8 @@ def measure_time():
|
|
|
137
136
|
t2p = time.perf_counter()
|
|
138
137
|
dt = (t2p - t1p) / 2
|
|
139
138
|
t = (t1 + t2) / 2
|
|
140
|
-
timing[
|
|
141
|
-
timing[
|
|
139
|
+
timing["time (unix)"] = t
|
|
140
|
+
timing["dt (s)"] = dt
|
|
142
141
|
|
|
143
142
|
|
|
144
143
|
@contextmanager
|
|
@@ -163,10 +162,10 @@ def measure_duration():
|
|
|
163
162
|
yield duration
|
|
164
163
|
finally:
|
|
165
164
|
t2 = time.perf_counter()
|
|
166
|
-
duration[
|
|
165
|
+
duration["duration (s)"] = t2 - t1
|
|
167
166
|
|
|
168
167
|
|
|
169
|
-
def after(duration=
|
|
168
|
+
def after(duration=":::", function=None, args=None, kwargs=None, blocking=True):
|
|
170
169
|
"""Execute function after given waiting time
|
|
171
170
|
|
|
172
171
|
Parameters
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
from threading import Thread
|
|
24
23
|
from functools import wraps
|
|
25
24
|
|
|
@@ -41,15 +40,17 @@ def cli(timer):
|
|
|
41
40
|
- 't' or 'time': print timing (interval, elapsed time, etc.) info
|
|
42
41
|
- 'q', 'Q', 'quit' or 'stop': stop timer and exit
|
|
43
42
|
"""
|
|
44
|
-
msg =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
msg = (
|
|
44
|
+
"-----------------------------------------------------------------\n"
|
|
45
|
+
"Timer Command-Line-Interface. Possible inputs:\n"
|
|
46
|
+
"- any number (int/float): change timer interval to that new value\n"
|
|
47
|
+
"- 'p' or 'pause': pause timer\n"
|
|
48
|
+
"- 'r' or 'resume': resume timer\n"
|
|
49
|
+
"- 'R' or 'reset': reset timer\n"
|
|
50
|
+
"- 't' or 'time': print timing (interval, elapsed time, etc.) info\n"
|
|
51
|
+
"- 'q', 'Q', 'quit' or 'stop': stop timer and exit\n"
|
|
52
|
+
"-----------------------------------------------------------------\n"
|
|
53
|
+
)
|
|
53
54
|
print(msg)
|
|
54
55
|
|
|
55
56
|
while not timer.is_stopped:
|
|
@@ -57,24 +58,26 @@ def cli(timer):
|
|
|
57
58
|
try:
|
|
58
59
|
dt = float(a)
|
|
59
60
|
except ValueError:
|
|
60
|
-
if a in (
|
|
61
|
-
print(
|
|
61
|
+
if a in ("p", "pause"):
|
|
62
|
+
print("--- Timer Paused")
|
|
62
63
|
timer.pause()
|
|
63
|
-
elif a in (
|
|
64
|
-
print(
|
|
64
|
+
elif a in ("r", "resume"):
|
|
65
|
+
print("--- Timer Resumed")
|
|
65
66
|
timer.resume()
|
|
66
|
-
elif a in (
|
|
67
|
-
print(
|
|
67
|
+
elif a in ("R", "reset"):
|
|
68
|
+
print("--- Timer Restarted")
|
|
68
69
|
timer.reset()
|
|
69
|
-
elif a in (
|
|
70
|
+
elif a in ("t", "time"):
|
|
70
71
|
elapsed = timer.elapsed_time
|
|
71
72
|
paused = timer.pause_time
|
|
72
73
|
dt = timer.interval
|
|
73
74
|
tnext = timer.next_checkpt_release - timer.now()
|
|
74
|
-
print(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
print(
|
|
76
|
+
"[Interval {:.3f}] [Elapsed: {:.3f}] [Paused {:.3f}] "
|
|
77
|
+
"[Next {:.3f}]".format(dt, elapsed, paused, tnext)
|
|
78
|
+
)
|
|
79
|
+
elif a in ("q", "Q", "quit", "stop"):
|
|
80
|
+
print("--- Timer Stopped")
|
|
78
81
|
timer.stop()
|
|
79
82
|
else:
|
|
80
83
|
pass
|
|
@@ -82,11 +85,11 @@ def cli(timer):
|
|
|
82
85
|
try:
|
|
83
86
|
timer.interval = dt
|
|
84
87
|
except ValueError:
|
|
85
|
-
print(
|
|
88
|
+
print("--- Invalid Interval")
|
|
86
89
|
else:
|
|
87
|
-
print(
|
|
90
|
+
print("--- Interval (s) changed to {}".format(dt))
|
|
88
91
|
|
|
89
|
-
print(
|
|
92
|
+
print("--- Loop Exited")
|
|
90
93
|
|
|
91
94
|
|
|
92
95
|
# ========== Decorators to repeat function periodically using Timer ==========
|
|
@@ -99,13 +102,16 @@ def loop(timer):
|
|
|
99
102
|
----------
|
|
100
103
|
timer : oclock.Timer object
|
|
101
104
|
"""
|
|
105
|
+
|
|
102
106
|
def decorator(function):
|
|
103
107
|
@wraps(function)
|
|
104
108
|
def wrapper(*args, **kwargs):
|
|
105
109
|
while not timer.is_stopped:
|
|
106
110
|
timer.checkpt()
|
|
107
111
|
function(*args, **kwargs)
|
|
112
|
+
|
|
108
113
|
return wrapper
|
|
114
|
+
|
|
109
115
|
return decorator
|
|
110
116
|
|
|
111
117
|
|
|
@@ -116,6 +122,7 @@ def interactiveloop(**timer_kwargs):
|
|
|
116
122
|
----------
|
|
117
123
|
any argument or keyword-argument taken by oclock.Timer(), e.g. 'interval'
|
|
118
124
|
"""
|
|
125
|
+
|
|
119
126
|
def decorator(function):
|
|
120
127
|
@wraps(function)
|
|
121
128
|
def wrapper(*args, **kwargs):
|
|
@@ -125,5 +132,7 @@ def interactiveloop(**timer_kwargs):
|
|
|
125
132
|
while not timer.is_stopped:
|
|
126
133
|
function(*args, **kwargs)
|
|
127
134
|
timer.checkpt()
|
|
135
|
+
|
|
128
136
|
return wrapper
|
|
137
|
+
|
|
129
138
|
return decorator
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
import time
|
|
24
23
|
from random import random
|
|
25
24
|
from queue import Queue
|
|
@@ -46,12 +45,11 @@ def constant_duration_loop(timer, q, fracmax=0.5, n=10):
|
|
|
46
45
|
ts.append(t0)
|
|
47
46
|
|
|
48
47
|
for i in range(n):
|
|
49
|
-
|
|
50
48
|
# wait for a random time between 0 and the total requested interval / 2
|
|
51
49
|
with measure_duration() as duration:
|
|
52
50
|
r = timer.interval * fracmax * random()
|
|
53
51
|
time.sleep(r)
|
|
54
|
-
sleeptime = duration[
|
|
52
|
+
sleeptime = duration["duration (s)"]
|
|
55
53
|
|
|
56
54
|
# this is where the timer adapts the wait time to the execution time
|
|
57
55
|
# of the lines above.
|
|
@@ -78,9 +76,9 @@ def performance_test(dt, nloops, fmax, plot=False, warnings=False, precise=False
|
|
|
78
76
|
timer = Timer(interval=dt, warnings=warnings, precise=precise)
|
|
79
77
|
q = Queue()
|
|
80
78
|
|
|
81
|
-
print(
|
|
79
|
+
print("Test Started")
|
|
82
80
|
ts, rs = constant_duration_loop(timer, q, fmax, nloops)
|
|
83
|
-
print(
|
|
81
|
+
print("Test Finished")
|
|
84
82
|
|
|
85
83
|
dts = np.diff(ts)
|
|
86
84
|
avg = dts.mean()
|
|
@@ -91,32 +89,43 @@ def performance_test(dt, nloops, fmax, plot=False, warnings=False, precise=False
|
|
|
91
89
|
print("Std dev (ms): {}".format(dev * 1000))
|
|
92
90
|
|
|
93
91
|
if plot:
|
|
94
|
-
|
|
95
92
|
import matplotlib.pyplot as plt
|
|
93
|
+
|
|
96
94
|
fig, ax = plt.subplots(figsize=(6, 4))
|
|
97
95
|
|
|
98
96
|
n = len(dts)
|
|
99
97
|
ii = range(1, n + 1)
|
|
100
98
|
|
|
101
|
-
ax.plot(
|
|
102
|
-
|
|
99
|
+
ax.plot(
|
|
100
|
+
[1, n],
|
|
101
|
+
[dt * 1000, dt * 1000],
|
|
102
|
+
"-",
|
|
103
|
+
c="antiquewhite",
|
|
104
|
+
linewidth=6,
|
|
105
|
+
label="requested duration",
|
|
106
|
+
)
|
|
103
107
|
|
|
104
|
-
ax.fill_between(ii, rs * 1000, label=
|
|
108
|
+
ax.fill_between(ii, rs * 1000, label="random time", color="0.9")
|
|
105
109
|
|
|
106
|
-
ax.plot(ii, dts * 1000,
|
|
107
|
-
label='individual loop duration')
|
|
110
|
+
ax.plot(ii, dts * 1000, ".", c="steelblue", label="individual loop duration")
|
|
108
111
|
|
|
109
|
-
ax.plot(
|
|
110
|
-
|
|
112
|
+
ax.plot(
|
|
113
|
+
[1, n],
|
|
114
|
+
[avg * 1000, avg * 1000],
|
|
115
|
+
":",
|
|
116
|
+
c="sandybrown",
|
|
117
|
+
linewidth=4,
|
|
118
|
+
label="average loop duration",
|
|
119
|
+
)
|
|
111
120
|
|
|
112
121
|
ax.set_xlim((1, n))
|
|
113
122
|
ax.set_ylim((0, dts.max() * 1050))
|
|
114
123
|
|
|
115
|
-
ax.set_xlabel(
|
|
116
|
-
ax.set_ylabel(r
|
|
124
|
+
ax.set_xlabel("loop number")
|
|
125
|
+
ax.set_ylabel(r"$\Delta t$ (ms)")
|
|
117
126
|
|
|
118
127
|
ax.grid()
|
|
119
128
|
ax.legend()
|
|
120
129
|
plt.show()
|
|
121
130
|
|
|
122
|
-
return {
|
|
131
|
+
return {"mean dt (s)": avg, "std dev (s)": dev}
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
# along with the oclock python package.
|
|
20
20
|
# If not, see <https://www.gnu.org/licenses/>
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
import time
|
|
24
23
|
import threading
|
|
25
24
|
from .event import Event
|
|
@@ -28,7 +27,7 @@ from .event import Event
|
|
|
28
27
|
class Timer:
|
|
29
28
|
"""Timer that is cancellable and modifiable in real time."""
|
|
30
29
|
|
|
31
|
-
def __init__(self, interval=1, name=
|
|
30
|
+
def __init__(self, interval=1, name="Timer", warnings=False, precise=False):
|
|
32
31
|
"""Init oclock.Timer object.
|
|
33
32
|
|
|
34
33
|
Parameters
|
|
@@ -60,13 +59,14 @@ class Timer:
|
|
|
60
59
|
# used to wait for timer reactivation when in a paused state
|
|
61
60
|
self._unpause_event = Event() if precise else threading.Event()
|
|
62
61
|
|
|
63
|
-
self._start()
|
|
62
|
+
self._start() # Timer starts automatically upon init
|
|
64
63
|
|
|
65
64
|
def __repr__(self):
|
|
66
65
|
"""Str representation of Timer object"""
|
|
67
|
-
warning =
|
|
68
|
-
s = "{}, name '{}', interval {}s, warnings {}"
|
|
69
|
-
|
|
66
|
+
warning = "ON" if self.warnings else "OFF"
|
|
67
|
+
s = "{}, name '{}', interval {}s, warnings {}".format(
|
|
68
|
+
self.__class__, self.name, self.interval, warning
|
|
69
|
+
)
|
|
70
70
|
return s
|
|
71
71
|
|
|
72
72
|
def _start(self):
|
|
@@ -91,7 +91,7 @@ class Timer:
|
|
|
91
91
|
"""Stop timer immediately."""
|
|
92
92
|
if self.is_paused:
|
|
93
93
|
self.resume()
|
|
94
|
-
self._unpause_event.set()
|
|
94
|
+
self._unpause_event.set() # in case stop is called in a paused state
|
|
95
95
|
self._bypass_checkpt.set() # cancel any remaining wait at the checkpt
|
|
96
96
|
self.stop_time = self.now()
|
|
97
97
|
self.is_stopped = True
|
|
@@ -117,7 +117,6 @@ class Timer:
|
|
|
117
117
|
"""Waits at current point in program to keep the interval constant."""
|
|
118
118
|
|
|
119
119
|
if self.is_paused: # if timer is paused, wait for reactivation by resume()
|
|
120
|
-
|
|
121
120
|
self._unpause_event.wait()
|
|
122
121
|
|
|
123
122
|
# The two lines below (target adjustment and else statement) make
|
|
@@ -126,16 +125,17 @@ class Timer:
|
|
|
126
125
|
self._target = self.now() + self._interval
|
|
127
126
|
|
|
128
127
|
else:
|
|
129
|
-
|
|
130
128
|
if not self.interval_exceeded:
|
|
131
|
-
|
|
132
129
|
# if time before the previous checkpt has not exceeded the
|
|
133
130
|
# required interval, set target to another multiple of dt
|
|
134
131
|
|
|
135
132
|
if self.warnings and self._interval_failed:
|
|
136
133
|
# only called when interval is ok again after having failed
|
|
137
|
-
print(
|
|
138
|
-
|
|
134
|
+
print(
|
|
135
|
+
"--- Time interval ({}s) OK again for {}".format(
|
|
136
|
+
self.interval, self.name
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
139
|
self._interval_failed = False
|
|
140
140
|
|
|
141
141
|
w = self._target - self.now()
|
|
@@ -143,14 +143,16 @@ class Timer:
|
|
|
143
143
|
self._bypass_checkpt.wait(w)
|
|
144
144
|
|
|
145
145
|
else:
|
|
146
|
-
|
|
147
146
|
# if already passed target, move on immediately and set target
|
|
148
147
|
# at a time dt from current time to try again.
|
|
149
148
|
|
|
150
149
|
if self.warnings and not self._interval_failed:
|
|
151
150
|
# only called when interval fails right after being ok
|
|
152
|
-
print(
|
|
153
|
-
|
|
151
|
+
print(
|
|
152
|
+
"--- Warning, time interval ({}s) too short for {}".format(
|
|
153
|
+
self.interval, self.name
|
|
154
|
+
)
|
|
155
|
+
)
|
|
154
156
|
self._interval_failed = True
|
|
155
157
|
|
|
156
158
|
self._target = self.now() + self.interval
|
|
@@ -192,7 +194,7 @@ class Timer:
|
|
|
192
194
|
def set_interval(self, value, immediate=True):
|
|
193
195
|
"""Choose if interval change is effective immediately or at next checkpt"""
|
|
194
196
|
if value < 0:
|
|
195
|
-
raise ValueError(
|
|
197
|
+
raise ValueError("Timer interval must be positive")
|
|
196
198
|
self._interval = value
|
|
197
199
|
if immediate:
|
|
198
200
|
self._target = self.now() + value
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: oclock
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.3
|
|
4
4
|
Summary: Tools for timed, no-drift loops of constant duration, and other misc. timing tools (GUI countdown, context managers etc.)
|
|
5
5
|
Author-email: Olivier Vincent <ovinc.py@gmail.com>
|
|
6
6
|
License: GNU GPLv3
|
|
@@ -15,6 +15,7 @@ Requires-Python: >=3.5
|
|
|
15
15
|
Description-Content-Type: text/markdown
|
|
16
16
|
License-File: LICENSE
|
|
17
17
|
Requires-Dist: importlib-metadata
|
|
18
|
+
Dynamic: license-file
|
|
18
19
|
|
|
19
20
|
# About
|
|
20
21
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|