Ordered Demultiplexer in Python
Single pass approach to Demultiplexing/Demuxing
Break an iterator into multiple iterators based on a set of filters.
Typical demuxers will place elements into different iterators, such as splitting [0,1,2,3] into ([0,2], [1,3]) based on odd or even elements. Ordered Demuxers focus on breaking iterators into contiguous blocks that are meant to be immediately worked upon, without having to iterate over the list more than once.
This makes them appropriate to use with iterators where the contents cannot be fully held in memory, such as retrieving data online.
Example
With any iterable input such as
x = iter([ (_, 0), (_, 1), (MessageEnd, 2), (_, 3), (_, 4), (MessageEnd, 5) ])
This can be broken into;
Iterator [
Iterator [(_, 0), (_, 1), (MessageEnd, 2)],
Iterator [(_, 3), (_, 4), (MessageEnd, 5)]
]
Without passing over each element of data multiple times. This allows for methods like;
for data_stream in demuxed_stream:
for element in data_stream:
function(element)
Or more interestingly;
def foo(x: Iterator[T]):
...
for data_stream in demuxed_stream:
foo(data_stream)
foo will consume part of the original iterator, up until the next break point, but still behave identically to passing it an iterator of just the data required.
Due to the way the filters are available within the Demuxer, it's also possible to send these partial iterators to functions according to the relevant filter, i.e.
conditions = [
FilterCondition(lambda x: x[0] == 'MessageEnd', 'SuccessfulMessageStream'),
FilterCondition(lambda x: x[0] == 'MessageFailed', 'FailedMessageStream')
]
for data_stream in demuxed_stream:
if demuxed_stream.condition_met is not None:
match demuxed_stream.condition_met.name:
case 'SuccessfulMessageStream':
foo(data_stream)
case 'FailedMessageStream':
foo2(data_stream)
else:
foo3(data_stream)
Although it requires the consumption of each iterator entirely to make this possible.
Installation
python -m pip install ordered-demuxer
Usage
>>> from ordered_demuxer import FilterCondition, OrderedDemuxer
>>> x = [1, 2, 3, 4]
>>> y = FilterCondition(lambda x: x == 2)
>>> splt = OrderedDemuxer(data_source=iter(x), filter=y, split_after=True)
>>> x_iter = splt.__next__()
>>> print(list(x_iter))
[1, 2]
Release files for ordered-demuxer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ordered_demuxer-0.1.0.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ordered_demuxer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.6 kB
Release files / ordered_demuxer-0.1.0.tar.gz
| Download URL | ordered_demuxer-0.1.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
96c220bcaf6c8f93f0e4e6e7cbcdeaa742043f28f24cdc0b0bc716f7f47ab440
|
|
BLAKE2b-256 checksum How to use checksums |
ac415fc2f384a904b4c4b65ca44078baa880d164daff9d6888fcbe1892d4b156
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Release files / ordered_demuxer-0.1.0-py3-none-any.whl
| Download URL | ordered_demuxer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
41c60299e0284d6056482336c70ea03bdae91c9b32a61966d9f41aab1d90fc3a
|
|
BLAKE2b-256 checksum How to use checksums |
8f7f4a2ddfab5c457e67b6cc6f2e819121a896d4663499463d76560121c83731
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|