stream2py 1.0.44__tar.gz → 1.0.46__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.44 → stream2py-1.0.46}/PKG-INFO +1 -1
- {stream2py-1.0.44 → stream2py-1.0.46}/pyproject.toml +1 -1
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/buffer_reader.py +37 -1
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/source_reader.py +26 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/stream_buffer.py +28 -1
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_buffer_reader_blocking.py +3 -3
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_buffer_reader_stopiteration.py +4 -9
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_reader_without_context.py +3 -5
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_stream_buffer.py +6 -3
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_stream_source.py +6 -3
- stream2py-1.0.46/stream2py/tests/test_zz_range_join.py +78 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/utils_for_testing.py +15 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/.gitattributes +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/.github/workflows/ci.yml +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/.gitignore +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/ISSUE_ANALYSIS.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/ISSUE_COMMENTS.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/LICENSE +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/PR_DESCRIPTION.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/README.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/README.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/RESOLVED_ISSUES.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/.gitignore +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/Makefile +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/conf.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/index.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/make.bat +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/buffer_reader.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/protocols.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/simply.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/source_reader.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/sources/http.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/sources.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/stream_buffer.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/stream_source.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_buffer_reader.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_lock_sorted_deque.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_source_reader.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_stream_buffer.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_stream_source.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_util.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/utils_for_testing.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/util.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/locked_sorted_deque.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/reader_writer_lock.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/sorted_collection.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/sorted_deque.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/typing_hints.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/docsrc/table_of_contents.rst +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/pack.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/__init__.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/examples/source_reader.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/examples/stream_source.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/exceptions.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/protocols.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/simply.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/sources/__init__.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/sources/http.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/stream_source.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/__init__.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_buffer_reader.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_lock_sorted_deque.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_quick_source_reader.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_source_reader.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/tests/test_util.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/util.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/__init__.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/locked_sorted_deque.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/reader_writer_lock.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/sorted_collection.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/sorted_deque.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/stream2py/utility/typing_hints.py +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/things_you_can_do.md +0 -0
- {stream2py-1.0.44 → stream2py-1.0.46}/use_cases.md +0 -0
|
@@ -122,6 +122,8 @@ class BufferReader:
|
|
|
122
122
|
self._buffer = buffer
|
|
123
123
|
self._last_item = None
|
|
124
124
|
self._last_key = None
|
|
125
|
+
self._join_items = None # set by the buffer from the source reader, if offered
|
|
126
|
+
self._slice_item = None
|
|
125
127
|
self._stop_event = stop_event
|
|
126
128
|
self._sleep_time_on_iter_none_s = 0.1
|
|
127
129
|
self.read_size = read_size # read_size used by __next__
|
|
@@ -237,6 +239,7 @@ class BufferReader:
|
|
|
237
239
|
only_new_items=False,
|
|
238
240
|
start_le=False,
|
|
239
241
|
stop_ge=False,
|
|
242
|
+
join=False,
|
|
240
243
|
):
|
|
241
244
|
"""Enables:
|
|
242
245
|
1. Get last n minutes
|
|
@@ -255,8 +258,26 @@ class BufferReader:
|
|
|
255
258
|
i.e. keys=[0, 10, 20], start=9 will include key=0
|
|
256
259
|
:param stop_ge: if True, raise ValueError when there is no key greater than or equal to
|
|
257
260
|
stop in buffer, if ignore_no_item_found is also True, return None instead of ValueError
|
|
258
|
-
:
|
|
261
|
+
:param join: if True, return ONE item instead of a list: the items in range
|
|
262
|
+
trimmed to exactly ``[start, stop]`` and joined, using the source reader's
|
|
263
|
+
``slice_item`` and ``join_items`` (both must be implemented, otherwise
|
|
264
|
+
NotImplementedError is raised before anything is read). Implies
|
|
265
|
+
``start_le=True`` so the chunk containing ``start`` is included, and is
|
|
266
|
+
incompatible with ``step``. The ``last_item`` cursor still moves to the
|
|
267
|
+
last *buffer* item, not the trimmed one. With ``only_new_items`` the
|
|
268
|
+
joined item starts after the cursor. When nothing is found the result is
|
|
269
|
+
the same as without ``join`` (``[]``, ``None`` or an exception).
|
|
270
|
+
:return: list of items in range (a single joined item if ``join``)
|
|
259
271
|
"""
|
|
272
|
+
if join:
|
|
273
|
+
if step is not None:
|
|
274
|
+
raise ValueError("range(join=True) doesn't support step")
|
|
275
|
+
if self._join_items is None or self._slice_item is None:
|
|
276
|
+
raise NotImplementedError(
|
|
277
|
+
"range(join=True) needs the source reader to implement both "
|
|
278
|
+
"join_items(items) and slice_item(item, start, stop)"
|
|
279
|
+
)
|
|
280
|
+
start_le = True
|
|
260
281
|
if ignore_no_item_found is None:
|
|
261
282
|
ignore_no_item_found = self._read_kwargs.get("ignore_no_item_found", False)
|
|
262
283
|
with self._buffer.reader_lock() as reader:
|
|
@@ -275,6 +296,9 @@ class BufferReader:
|
|
|
275
296
|
_start = start if start > _next_key else _next_key
|
|
276
297
|
else:
|
|
277
298
|
_start = start
|
|
299
|
+
trim_start = (
|
|
300
|
+
_start # where the joined item must begin, before start_le widens
|
|
301
|
+
)
|
|
278
302
|
if start_le is True:
|
|
279
303
|
with suppress(
|
|
280
304
|
ValueError
|
|
@@ -299,8 +323,20 @@ class BufferReader:
|
|
|
299
323
|
if ignore_no_item_found:
|
|
300
324
|
return None
|
|
301
325
|
raise e
|
|
326
|
+
if join and items:
|
|
327
|
+
return self._trim_and_join(items, trim_start, stop)
|
|
302
328
|
return items
|
|
303
329
|
|
|
330
|
+
def _trim_and_join(self, items, start, stop):
|
|
331
|
+
"""Trim the first and last of ``items`` to ``[start, stop]`` and join them.
|
|
332
|
+
|
|
333
|
+
Only the ends can overhang the query (the keys are ordered), and a single
|
|
334
|
+
item is both ends at once."""
|
|
335
|
+
items = list(items)
|
|
336
|
+
items[0] = self._slice_item(items[0], start, None)
|
|
337
|
+
items[-1] = self._slice_item(items[-1], None, stop)
|
|
338
|
+
return self._join_items(items)
|
|
339
|
+
|
|
304
340
|
def tail(self, *, peek=False, ignore_no_item_found=False, only_new_items=False):
|
|
305
341
|
"""Finds the last item in buffer. Raise ValueError if no item found.
|
|
306
342
|
|
|
@@ -129,6 +129,32 @@ class SourceReader(Source, metaclass=ABCMeta):
|
|
|
129
129
|
"Implement the 'key' method to convert data into a comparable value to sort by"
|
|
130
130
|
)
|
|
131
131
|
|
|
132
|
+
def join_items(self, items: list) -> Any:
|
|
133
|
+
"""(Optional) Join consecutive data items into one data item.
|
|
134
|
+
|
|
135
|
+
Implement together with ``slice_item`` to let ``BufferReader.range(...,
|
|
136
|
+
join=True)`` return the exact span asked for, as a single item (e.g. several
|
|
137
|
+
waveform chunks concatenated). Not abstract: sources whose items can't be
|
|
138
|
+
joined just don't override it.
|
|
139
|
+
|
|
140
|
+
:param items: non-empty list of data items, in key order
|
|
141
|
+
:return: one data item
|
|
142
|
+
"""
|
|
143
|
+
raise NotImplementedError("Implement 'join_items' to support range(join=True)")
|
|
144
|
+
|
|
145
|
+
def slice_item(self, item: Any, start=None, stop=None) -> Any:
|
|
146
|
+
"""(Optional) The part of ``item`` whose keys fall within ``[start, stop]``.
|
|
147
|
+
|
|
148
|
+
``None`` for a bound means unbounded on that side; an item that needs no
|
|
149
|
+
trimming can be returned as is. See ``join_items``. Must be pure (items in the
|
|
150
|
+
buffer are shared, never mutate them) and keyed on absolute keys: it is applied
|
|
151
|
+
once per bound, so a lone item is sliced twice (``start`` then ``stop``).
|
|
152
|
+
|
|
153
|
+
:param item: a data item as returned by ``read``
|
|
154
|
+
:return: a data item of the same kind, possibly shorter
|
|
155
|
+
"""
|
|
156
|
+
raise NotImplementedError("Implement 'slice_item' to support range(join=True)")
|
|
157
|
+
|
|
132
158
|
@property
|
|
133
159
|
def closed(self) -> bool:
|
|
134
160
|
"""True if the source reader is closed, False if it's open.
|
|
@@ -24,6 +24,21 @@ DFLT_SLEEP_TIME_ON_READ_NONE_S = 0.3
|
|
|
24
24
|
DFLT_MAX_LEN = 10000
|
|
25
25
|
|
|
26
26
|
|
|
27
|
+
def _overridden(source_reader, name):
|
|
28
|
+
"""The bound method ``name`` if the source reader really implements it, else None.
|
|
29
|
+
|
|
30
|
+
``SourceReader`` defines ``join_items``/``slice_item`` as raising stubs, so
|
|
31
|
+
presence on the object doesn't mean support."""
|
|
32
|
+
method = getattr(source_reader, name, None)
|
|
33
|
+
if method is None:
|
|
34
|
+
return None
|
|
35
|
+
from stream2py.source_reader import SourceReader
|
|
36
|
+
|
|
37
|
+
if getattr(type(source_reader), name, None) is getattr(SourceReader, name, None):
|
|
38
|
+
return None
|
|
39
|
+
return method
|
|
40
|
+
|
|
41
|
+
|
|
27
42
|
class _SourceBuffer:
|
|
28
43
|
def __init__(
|
|
29
44
|
self,
|
|
@@ -33,6 +48,8 @@ class _SourceBuffer:
|
|
|
33
48
|
key=None,
|
|
34
49
|
maxlen: int = 10000,
|
|
35
50
|
buffer_reader_class: Type[BufferReader] = BufferReader,
|
|
51
|
+
join_items=None,
|
|
52
|
+
slice_item=None,
|
|
36
53
|
):
|
|
37
54
|
"""StreamBuffer helper class
|
|
38
55
|
Thread safe buffer for reading and writing data items
|
|
@@ -46,6 +63,8 @@ class _SourceBuffer:
|
|
|
46
63
|
self._buffer = RWLockSortedDeque([], key=key, maxlen=maxlen)
|
|
47
64
|
self._source_reader_info = source_reader_info
|
|
48
65
|
self.buffer_reader_class = buffer_reader_class
|
|
66
|
+
self._join_items = join_items
|
|
67
|
+
self._slice_item = slice_item
|
|
49
68
|
|
|
50
69
|
def __len__(self):
|
|
51
70
|
return len(self._buffer)
|
|
@@ -67,16 +86,22 @@ class _SourceBuffer:
|
|
|
67
86
|
writer.drop(n)
|
|
68
87
|
|
|
69
88
|
def mk_reader(self, **read_kwargs):
|
|
70
|
-
|
|
89
|
+
reader = self.buffer_reader_class(
|
|
71
90
|
buffer=self._buffer,
|
|
72
91
|
source_reader_info=self._source_reader_info,
|
|
73
92
|
stop_event=self._stop_event,
|
|
74
93
|
**read_kwargs,
|
|
75
94
|
)
|
|
95
|
+
# set after construction so custom buffer_reader_classes need not know about them
|
|
96
|
+
reader._join_items = self._join_items
|
|
97
|
+
reader._slice_item = self._slice_item
|
|
98
|
+
return reader
|
|
76
99
|
|
|
77
100
|
def attach_reader(self, reader):
|
|
78
101
|
reader._buffer = self._buffer
|
|
79
102
|
reader._stop_event = self._stop_event
|
|
103
|
+
reader._join_items = self._join_items
|
|
104
|
+
reader._slice_item = self._slice_item
|
|
80
105
|
|
|
81
106
|
|
|
82
107
|
class StreamBuffer:
|
|
@@ -293,6 +318,8 @@ class StreamBuffer:
|
|
|
293
318
|
buffer_reader_class=getattr(
|
|
294
319
|
self.source_reader, "buffer_reader_class", BufferReader
|
|
295
320
|
),
|
|
321
|
+
join_items=_overridden(self.source_reader, "join_items"),
|
|
322
|
+
slice_item=_overridden(self.source_reader, "slice_item"),
|
|
296
323
|
)
|
|
297
324
|
|
|
298
325
|
# def _mk_contextualized_iterator(self):
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import time
|
|
3
3
|
import threading
|
|
4
4
|
from stream2py import StreamBuffer
|
|
5
|
-
from stream2py.tests.utils_for_testing import SimpleSourceReader
|
|
5
|
+
from stream2py.tests.utils_for_testing import SimpleSourceReader, wait_until
|
|
6
6
|
from itertools import count
|
|
7
7
|
|
|
8
8
|
|
|
@@ -15,7 +15,7 @@ def test_buffer_reader_blocking():
|
|
|
15
15
|
reader = buffer.mk_reader()
|
|
16
16
|
|
|
17
17
|
# Wait a moment for some data to be available
|
|
18
|
-
|
|
18
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 6)
|
|
19
19
|
|
|
20
20
|
# Non-blocking read should return data immediately when available
|
|
21
21
|
result = reader.read(blocking=False, ignore_no_item_found=True)
|
|
@@ -47,7 +47,7 @@ def test_buffer_reader_blocking_stops_on_stop_event():
|
|
|
47
47
|
reader = buffer.mk_reader()
|
|
48
48
|
|
|
49
49
|
# Read all available data
|
|
50
|
-
|
|
50
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 5)
|
|
51
51
|
while reader.read(blocking=False, ignore_no_item_found=True) is not None:
|
|
52
52
|
pass
|
|
53
53
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Tests for BufferReader StopIteration behavior (Issue #10)"""
|
|
2
2
|
import pytest
|
|
3
3
|
from stream2py import StreamBuffer
|
|
4
|
-
from stream2py.tests.utils_for_testing import SimpleSourceReader
|
|
4
|
+
from stream2py.tests.utils_for_testing import SimpleSourceReader, wait_until
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def test_next_raises_stopiteration_when_stopped():
|
|
@@ -38,8 +38,7 @@ def test_next_returns_none_when_no_data_but_running():
|
|
|
38
38
|
reader = buffer.mk_reader()
|
|
39
39
|
|
|
40
40
|
# Read all available data quickly
|
|
41
|
-
|
|
42
|
-
time.sleep(0.1) # Let some data accumulate
|
|
41
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 1)
|
|
43
42
|
|
|
44
43
|
# Read until we get None (no more data available yet)
|
|
45
44
|
items = []
|
|
@@ -63,8 +62,7 @@ def test_builtin_next_with_default():
|
|
|
63
62
|
buffer.start()
|
|
64
63
|
reader = buffer.mk_reader()
|
|
65
64
|
|
|
66
|
-
|
|
67
|
-
time.sleep(0.1)
|
|
65
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 2)
|
|
68
66
|
|
|
69
67
|
# Read items
|
|
70
68
|
item1 = next(reader, 'default')
|
|
@@ -78,8 +76,6 @@ def test_builtin_next_with_default():
|
|
|
78
76
|
|
|
79
77
|
# When stopped and no data, should raise StopIteration
|
|
80
78
|
# and the default should be returned by builtin next()
|
|
81
|
-
time.sleep(0.1)
|
|
82
|
-
|
|
83
79
|
# Consume any remaining data
|
|
84
80
|
while True:
|
|
85
81
|
try:
|
|
@@ -102,8 +98,7 @@ def test_for_loop_stops_correctly():
|
|
|
102
98
|
buffer = StreamBuffer(source, maxlen=100)
|
|
103
99
|
buffer.start()
|
|
104
100
|
|
|
105
|
-
|
|
106
|
-
time.sleep(0.2) # Let all data load
|
|
101
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 10) # all data loaded
|
|
107
102
|
|
|
108
103
|
buffer.stop() # Stop before iterating
|
|
109
104
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Tests for using readers without context manager (Issue #18)"""
|
|
2
2
|
import pytest
|
|
3
3
|
from stream2py import StreamBuffer
|
|
4
|
-
from stream2py.tests.utils_for_testing import SimpleSourceReader
|
|
4
|
+
from stream2py.tests.utils_for_testing import SimpleSourceReader, wait_until
|
|
5
5
|
from stream2py.exceptions import StreamNotStartedError
|
|
6
6
|
|
|
7
7
|
|
|
@@ -26,8 +26,7 @@ def test_reader_works_without_context_manager():
|
|
|
26
26
|
reader = buffer.mk_reader()
|
|
27
27
|
|
|
28
28
|
# Reading should work fine without context manager
|
|
29
|
-
|
|
30
|
-
time.sleep(0.1) # Let some data accumulate
|
|
29
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 2) # some data accumulated
|
|
31
30
|
|
|
32
31
|
item = reader.read(ignore_no_item_found=True)
|
|
33
32
|
assert item is not None
|
|
@@ -84,8 +83,7 @@ def test_stream_buffer_with_context():
|
|
|
84
83
|
with StreamBuffer(source, maxlen=100) as buffer:
|
|
85
84
|
reader = buffer.mk_reader()
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
time.sleep(0.1)
|
|
86
|
+
assert wait_until(lambda: len(buffer.source_buffer) >= 1)
|
|
89
87
|
|
|
90
88
|
item = reader.read(ignore_no_item_found=True)
|
|
91
89
|
assert item is not None
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
from stream2py.tests.utils_for_testing import
|
|
1
|
+
from stream2py.tests.utils_for_testing import (
|
|
2
|
+
TenthSecondCounterStreamSource,
|
|
3
|
+
wait_until,
|
|
4
|
+
)
|
|
2
5
|
from stream2py.stream_buffer import StreamBuffer
|
|
3
6
|
import time
|
|
4
7
|
|
|
@@ -12,7 +15,7 @@ def test_stream_buffer():
|
|
|
12
15
|
sc_reader = (
|
|
13
16
|
sc_buf.mk_reader()
|
|
14
17
|
) # reader must be made after start to have data related to said start
|
|
15
|
-
|
|
18
|
+
assert wait_until(lambda: len(sc_buf.source_buffer) >= 20) # ~2s of data
|
|
16
19
|
assert sc_reader.source_reader_info['bt'] != 0, sc_reader.source_reader_info
|
|
17
20
|
print('data as shown is (timestamp, count) at every tenth of a second')
|
|
18
21
|
while True:
|
|
@@ -87,7 +90,7 @@ def test_stream_buffer():
|
|
|
87
90
|
), 'new readers should now have the same last_item cursor position'
|
|
88
91
|
|
|
89
92
|
# print some data from old and new reader and see they are different
|
|
90
|
-
|
|
93
|
+
assert wait_until(lambda: len(sc_buf.source_buffer) >= 3)
|
|
91
94
|
print('comparing first start reader values to second start')
|
|
92
95
|
while True:
|
|
93
96
|
r22 = sc_reader22.read(n=1, ignore_no_item_found=True)
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
from stream2py.tests.utils_for_testing import
|
|
1
|
+
from stream2py.tests.utils_for_testing import (
|
|
2
|
+
TenthSecondCounterStreamSource,
|
|
3
|
+
wait_until,
|
|
4
|
+
)
|
|
2
5
|
import time
|
|
3
6
|
|
|
4
7
|
|
|
@@ -6,7 +9,7 @@ def test_stream_source():
|
|
|
6
9
|
source = TenthSecondCounterStreamSource() # source
|
|
7
10
|
with source.open_reader() as sc_reader:
|
|
8
11
|
sc_buf = source._stream_buffer
|
|
9
|
-
|
|
12
|
+
assert wait_until(lambda: len(sc_buf.source_buffer) >= 20) # ~2s of data
|
|
10
13
|
assert source.info['bt'] != 0, source.info
|
|
11
14
|
print('data as shown is (timestamp, count) at every tenth of a second')
|
|
12
15
|
while True:
|
|
@@ -85,7 +88,7 @@ def test_stream_source():
|
|
|
85
88
|
), 'new readers should now have the same last_item cursor position'
|
|
86
89
|
|
|
87
90
|
# print some data from old and new reader and see they are different
|
|
88
|
-
|
|
91
|
+
assert wait_until(lambda: len(sc_buf.source_buffer) >= 3)
|
|
89
92
|
print('comparing first start reader values to second start')
|
|
90
93
|
while True:
|
|
91
94
|
r22 = sc_reader22.read(n=1, ignore_no_item_found=True)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""range(join=True) (i2mint/stream2py#14).
|
|
2
|
+
|
|
3
|
+
Deliberately thread-free: the buffer and reader are driven directly, so these tests
|
|
4
|
+
can't compete for CPU with the timing-sensitive threaded tests around them."""
|
|
5
|
+
import threading
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from stream2py import SourceReader
|
|
10
|
+
from stream2py.stream_buffer import _SourceBuffer, _overridden
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Chunks(SourceReader):
|
|
14
|
+
"""Items are (first_key, [values...]); value i sits at key first_key + i."""
|
|
15
|
+
|
|
16
|
+
def open(self):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
def read(self):
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
def close(self):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
def key(self, data):
|
|
26
|
+
return data[0]
|
|
27
|
+
|
|
28
|
+
def slice_item(self, item, start=None, stop=None):
|
|
29
|
+
first, values = item
|
|
30
|
+
lo = 0 if start is None else max(start - first, 0)
|
|
31
|
+
hi = len(values) if stop is None else stop - first + 1
|
|
32
|
+
return (first + lo, values[lo:hi])
|
|
33
|
+
|
|
34
|
+
def join_items(self, items):
|
|
35
|
+
return (items[0][0], [v for _, vs in items for v in vs])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class NoJoin(Chunks):
|
|
39
|
+
slice_item = SourceReader.slice_item
|
|
40
|
+
join_items = SourceReader.join_items
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _reader(source, n=5, size=4):
|
|
44
|
+
sb = _SourceBuffer(
|
|
45
|
+
{},
|
|
46
|
+
threading.Event(),
|
|
47
|
+
key=source.key,
|
|
48
|
+
join_items=_overridden(source, "join_items"),
|
|
49
|
+
slice_item=_overridden(source, "slice_item"),
|
|
50
|
+
)
|
|
51
|
+
for i in range(n):
|
|
52
|
+
sb.append((i * size, list(range(i * size, (i + 1) * size))))
|
|
53
|
+
return sb.mk_reader()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_range_join_trims_and_joins_chunks():
|
|
57
|
+
reader = _reader(Chunks())
|
|
58
|
+
assert reader.range(6, 13, join=True) == (6, list(range(6, 14)))
|
|
59
|
+
assert reader.last_key == 12 # cursor stays on a real buffer item
|
|
60
|
+
assert reader.range(5, 5, join=True, peek=True) == (5, [5])
|
|
61
|
+
assert reader.range(0, 19, join=True) == (0, list(range(20)))
|
|
62
|
+
# default behaviour is unchanged: whole chunks, from the first key >= start
|
|
63
|
+
assert [k for k, _ in reader.range(6, 13)] == [8, 12]
|
|
64
|
+
with pytest.raises(ValueError):
|
|
65
|
+
reader.range(0, 5, step=2, join=True)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_range_join_only_new_items_starts_after_cursor():
|
|
69
|
+
reader = _reader(Chunks())
|
|
70
|
+
reader.range(0, 5) # cursor now on the chunk with key 4
|
|
71
|
+
assert reader.last_key == 4
|
|
72
|
+
assert reader.range(0, 13, join=True, only_new_items=True)[0] == 8
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_range_join_needs_the_source_to_implement_it():
|
|
76
|
+
assert _overridden(NoJoin(), "join_items") is None
|
|
77
|
+
with pytest.raises(NotImplementedError):
|
|
78
|
+
_reader(NoJoin()).range(0, 5, join=True)
|
|
@@ -271,3 +271,18 @@ class TenthSecondCounterStreamSource(StreamSource):
|
|
|
271
271
|
return None
|
|
272
272
|
|
|
273
273
|
key = operator.itemgetter(0) # (timestamp_us, count) -> timestamp_us
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def wait_until(condition, *, timeout=10.0, interval=0.01):
|
|
277
|
+
"""Poll ``condition()`` until it is truthy or ``timeout`` seconds pass.
|
|
278
|
+
|
|
279
|
+
Use instead of a fixed ``time.sleep`` before asserting on data produced by a
|
|
280
|
+
background thread: a fixed sleep is too short on a loaded (or single-core) runner.
|
|
281
|
+
Returns the final truthiness of ``condition()`` so callers can ``assert`` it.
|
|
282
|
+
"""
|
|
283
|
+
deadline = time.monotonic() + timeout
|
|
284
|
+
while not condition():
|
|
285
|
+
if time.monotonic() >= deadline:
|
|
286
|
+
return bool(condition())
|
|
287
|
+
time.sleep(interval)
|
|
288
|
+
return True
|
|
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.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_buffer_reader.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_lock_sorted_deque.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_source_reader.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_stream_buffer.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/test_stream_source.rst
RENAMED
|
File without changes
|
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/tests/utils_for_testing.rst
RENAMED
|
File without changes
|
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/locked_sorted_deque.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/docsrc/module_docs/stream2py/utility/reader_writer_lock.rst
RENAMED
|
File without changes
|
{stream2py-1.0.44 → stream2py-1.0.46}/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
|