stream2py 1.0.43__tar.gz → 1.0.44__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.
- {stream2py-1.0.43 → stream2py-1.0.44}/PKG-INFO +1 -1
- {stream2py-1.0.43 → stream2py-1.0.44}/pyproject.toml +1 -1
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/__init__.py +1 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_util.py +78 -1
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/util.py +78 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/.gitattributes +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/.github/workflows/ci.yml +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/.gitignore +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/ISSUE_ANALYSIS.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/ISSUE_COMMENTS.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/LICENSE +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/PR_DESCRIPTION.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/README.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/README.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/RESOLVED_ISSUES.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/.gitignore +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/Makefile +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/conf.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/index.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/make.bat +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/buffer_reader.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/protocols.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/simply.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/source_reader.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/sources/http.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/sources.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/stream_buffer.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/stream_source.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_buffer_reader.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_lock_sorted_deque.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_source_reader.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_stream_buffer.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_stream_source.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_util.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/utils_for_testing.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/util.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/locked_sorted_deque.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/reader_writer_lock.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/sorted_collection.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/sorted_deque.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/typing_hints.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/docsrc/table_of_contents.rst +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/pack.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/buffer_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/examples/source_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/examples/stream_source.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/exceptions.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/protocols.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/simply.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/source_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/sources/__init__.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/sources/http.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/stream_buffer.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/stream_source.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/__init__.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_buffer_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_buffer_reader_blocking.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_buffer_reader_stopiteration.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_lock_sorted_deque.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_quick_source_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_reader_without_context.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_source_reader.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_stream_buffer.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/test_stream_source.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/tests/utils_for_testing.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/__init__.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/locked_sorted_deque.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/reader_writer_lock.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/sorted_collection.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/sorted_deque.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/stream2py/utility/typing_hints.py +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/things_you_can_do.md +0 -0
- {stream2py-1.0.43 → stream2py-1.0.44}/use_cases.md +0 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""Testing the util.py module"""
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from stream2py.util import contextualize_with_instance, Timer
|
|
4
6
|
|
|
5
7
|
|
|
6
8
|
class StreamHasNotBeenStarted(RuntimeError):
|
|
@@ -126,3 +128,78 @@ def test_contextualize_with_instance():
|
|
|
126
128
|
it_worked = False
|
|
127
129
|
|
|
128
130
|
assert it_worked # Hurray!
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# ---------------------------------------------------------------------------------------
|
|
134
|
+
# Timer
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def test_timer_manual_start_stop():
|
|
138
|
+
"""A manually started timer reports a positive, monotonically growing elapsed time."""
|
|
139
|
+
timer = Timer()
|
|
140
|
+
timer.start()
|
|
141
|
+
first = timer.elapsed()
|
|
142
|
+
assert first >= 0
|
|
143
|
+
second = timer.elapsed()
|
|
144
|
+
assert second >= first # monotonic clock: never goes backwards
|
|
145
|
+
timer.stop()
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_timer_as_context_manager():
|
|
149
|
+
"""Entering the context starts the timer and yields the timer itself."""
|
|
150
|
+
with Timer() as timer:
|
|
151
|
+
assert isinstance(timer, Timer)
|
|
152
|
+
assert timer.elapsed() >= 0
|
|
153
|
+
# leaving the context stops it
|
|
154
|
+
assert timer.start_time is None
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_timer_egress_is_applied():
|
|
158
|
+
"""The egress function transforms the elapsed seconds."""
|
|
159
|
+
with Timer(lambda seconds: 'transformed') as timer:
|
|
160
|
+
assert timer.elapsed() == 'transformed'
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_timer_is_reusable_across_contexts():
|
|
164
|
+
"""The same instance can be re-entered after being stopped."""
|
|
165
|
+
timer = Timer()
|
|
166
|
+
with timer:
|
|
167
|
+
pass
|
|
168
|
+
assert timer.start_time is None
|
|
169
|
+
with timer:
|
|
170
|
+
assert timer.elapsed() >= 0
|
|
171
|
+
assert timer.start_time is None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_timer_elapsed_before_start_raises_informatively():
|
|
175
|
+
"""Asking a stopped timer for elapsed time raises ValueError, not TypeError.
|
|
176
|
+
|
|
177
|
+
This is the regression guard for the original implementation, which detected the
|
|
178
|
+
not-started case by catching TypeError from ``time() - None``. That conflated "timer
|
|
179
|
+
not started" with "egress itself raised TypeError" -- see the test below.
|
|
180
|
+
"""
|
|
181
|
+
with pytest.raises(ValueError, match='not running'):
|
|
182
|
+
Timer().elapsed()
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def test_timer_does_not_swallow_egress_errors():
|
|
186
|
+
"""An exception raised by egress propagates, rather than being silently dropped.
|
|
187
|
+
|
|
188
|
+
The original implementation wrapped the whole computation in ``try/except TypeError``
|
|
189
|
+
and, when ``start_time`` was not None, fell off the end of the function -- returning
|
|
190
|
+
None and hiding the real error.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def broken_egress(seconds):
|
|
194
|
+
raise TypeError('egress is broken')
|
|
195
|
+
|
|
196
|
+
with Timer(broken_egress) as timer:
|
|
197
|
+
with pytest.raises(TypeError, match='egress is broken'):
|
|
198
|
+
timer.elapsed()
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def test_timer_stop_accepts_context_manager_exit_args():
|
|
202
|
+
"""__exit__ is stop(), so it must tolerate the (exc_type, exc, tb) triple."""
|
|
203
|
+
timer = Timer().start()
|
|
204
|
+
timer.stop(ValueError, ValueError('x'), None)
|
|
205
|
+
assert timer.start_time is None
|
|
@@ -5,6 +5,84 @@
|
|
|
5
5
|
from inspect import signature, Parameter
|
|
6
6
|
from abc import abstractmethod
|
|
7
7
|
from functools import wraps
|
|
8
|
+
from time import perf_counter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def identity(x):
|
|
12
|
+
"""Returns the input, as is."""
|
|
13
|
+
return x
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Timer:
|
|
17
|
+
"""A simple timer that can be used as a context manager, or started and stopped
|
|
18
|
+
manually.
|
|
19
|
+
|
|
20
|
+
>>> from time import sleep
|
|
21
|
+
>>> timer = Timer()
|
|
22
|
+
>>> timer.start() # doctest: +SKIP
|
|
23
|
+
>>> sleep(0.01)
|
|
24
|
+
>>> timer.elapsed() # doctest: +SKIP
|
|
25
|
+
0.010056478977203369
|
|
26
|
+
>>> timer.stop()
|
|
27
|
+
|
|
28
|
+
You can also use a context block, and specify an ``egress`` function that will be
|
|
29
|
+
called on the elapsed seconds:
|
|
30
|
+
|
|
31
|
+
>>> with Timer(lambda x: print(f"{int(x / (60 * 60 * 24))} days elapsed")) as timer:
|
|
32
|
+
... sleep(0.01)
|
|
33
|
+
... timer.elapsed()
|
|
34
|
+
0 days elapsed
|
|
35
|
+
|
|
36
|
+
The same timer instance can be reused across context blocks:
|
|
37
|
+
|
|
38
|
+
>>> timer = Timer(lambda x: int(x / 3600))
|
|
39
|
+
>>> with timer:
|
|
40
|
+
... sleep(0.01)
|
|
41
|
+
... t = timer.elapsed()
|
|
42
|
+
>>> t
|
|
43
|
+
0
|
|
44
|
+
>>> with timer:
|
|
45
|
+
... sleep(0.01)
|
|
46
|
+
|
|
47
|
+
Asking for the elapsed time of a timer that isn't running tells you so, instead of
|
|
48
|
+
failing obscurely:
|
|
49
|
+
|
|
50
|
+
>>> Timer().elapsed() # doctest: +ELLIPSIS
|
|
51
|
+
Traceback (most recent call last):
|
|
52
|
+
...
|
|
53
|
+
ValueError: The timer is not running....
|
|
54
|
+
|
|
55
|
+
Note that elapsed time is measured with ``time.perf_counter``, a monotonic clock, so
|
|
56
|
+
it is unaffected by system clock adjustments. This also means ``start_time`` is not a
|
|
57
|
+
wall-clock timestamp; it is only meaningful as a reference point for a difference.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def __init__(self, egress=identity):
|
|
61
|
+
self.egress = egress
|
|
62
|
+
self.start_time = None
|
|
63
|
+
|
|
64
|
+
def start(self):
|
|
65
|
+
self.start_time = perf_counter()
|
|
66
|
+
return self
|
|
67
|
+
|
|
68
|
+
def stop(self, *args, **kwargs):
|
|
69
|
+
self.start_time = None
|
|
70
|
+
|
|
71
|
+
def elapsed(self):
|
|
72
|
+
"""The (egress-transformed) seconds since the timer was started.
|
|
73
|
+
|
|
74
|
+
Raises ``ValueError`` if the timer is not running.
|
|
75
|
+
"""
|
|
76
|
+
if self.start_time is None:
|
|
77
|
+
raise ValueError(
|
|
78
|
+
"The timer is not running, so it has no elapsed time. "
|
|
79
|
+
"Start it with timer.start(), or use a context block "
|
|
80
|
+
"(`with timer: ...` or `with Timer() as timer: ...`)."
|
|
81
|
+
)
|
|
82
|
+
return self.egress(perf_counter() - self.start_time)
|
|
83
|
+
|
|
84
|
+
__enter__ = start
|
|
85
|
+
__exit__ = stop
|
|
8
86
|
|
|
9
87
|
|
|
10
88
|
class TypeValidationError(TypeError):
|
|
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
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_buffer_reader.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_lock_sorted_deque.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_source_reader.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_stream_buffer.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/test_stream_source.rst
RENAMED
|
File without changes
|
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/tests/utils_for_testing.rst
RENAMED
|
File without changes
|
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/locked_sorted_deque.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/reader_writer_lock.rst
RENAMED
|
File without changes
|
{stream2py-1.0.43 → stream2py-1.0.44}/docsrc/module_docs/stream2py/utility/sorted_collection.rst
RENAMED
|
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
|
|
File without changes
|