From bf594b83a00436e3f80c97944098d46b68ab7342 Mon Sep 17 00:00:00 2001 From: Thor Whalen <1906276+thorwhalen@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:21:31 +0100 Subject: [PATCH 1/2] README: what stream2py is, install, a first runnable example The README had no code at all. It now opens with what stream2py does (buffer a source once, hand out independent readers), the install line, and a runnable example with two readers. Completes the 'Go back in time' section, which stopped mid-sentence, fixes typos and unwraps prose. --- README.md | 81 +++++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index caf7e25..5561e29 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,35 @@ +# stream2py -[Documentation hosted here](https://i2mint.github.io/stream2py/index.html). +Bring data streams to Python, with ease. Wrap a source (a microphone, a sensor, a socket, a keyboard) once, and stream2py buffers it in a background thread and hands out as many independent readers as you need, so you can store, plot and analyse the same stream at the same time without losing data. -To install: `pip install stream2py` +``` +pip install stream2py +``` -# stream2py +No dependencies (standard library only). [Documentation](https://i2mint.github.io/stream2py/index.html). + +```python +from stream2py import StreamBuffer +from stream2py.examples.source_reader import SimpleCounterString + +source = SimpleCounterString(start=0, stop=100) # a toy source that reads 's0', 's1', ... -Bring data streams to python, with ease. +with StreamBuffer(source_reader=source, maxlen=100) as buffer: + saver = buffer.mk_reader() # one consumer, say, writing to disk + plotter = buffer.mk_reader() # another, drawing a live plot + assert (saver.next(), saver.next()) == ('s0', 's1') + assert plotter.next() == 's0' # each reader has its own cursor +``` -One of the goals of the suite of i2i tools is to get from idea 2 implementation without all the fuss. -We've got py2store to do that for the storage (reading or writing) concern, -and others (e.g. py2cli, py2ws, py2dash) to take care of exposing python functions to the world (of command line interfaces, webservices, browser dashboards, etc.). +A real source is any object with `open`, `read`, `close` and a `key` to order reads by (the `stream2py.protocols.Source` protocol; plugins below ship ready-made ones), or, for quick cases, three functions given to `stream2py.simply.mk_stream_buffer`. -Here, we address the stream acquisition concern. -As always, we aim at offering as-simple-as-drawing-a-simple-drawing means to get things done. +stream2py is part of the i2i tools ("idea to implementation"): `dol` and `py2store` handle the storage concern, `py2http`, `front` and others expose functions to the world, and stream2py handles stream acquisition. ## Plugins `stream2py` has the core functionality, and is completely dependency-free (only builtin python). -That said, to work with specific stream sources, you can install plugins that will allow you to work with them. -At the time of writing this we have: +That said, to work with specific stream sources, you can install plugins that will allow you to work with them. At the time of writing this we have: - Audio: [audiostream2py](https://github.com/i2mint/audiostream2py) - Keyboard inputs: [keyboardstream2py](https://github.com/i2mint/keyboardstream2py) - PLC (Programmable Logic Controller): [plcstream2py](https://github.com/i2mint/plcstream2py) @@ -32,17 +42,9 @@ At the time of writing this we have: ## Reduce vocabulary entropy -One way we do this is by reducing the vocabulary entropy: -We don't want to have to think about how every specific source calls a read, or a size, -or a time to pause before reads, -or what format THAT particular sensor is encoding it's data in, -having you shuffle through documentation pages before you can figure out how to start doing -the fun stuff, -which happens to be the stuff that actually produces value. And, oh, once you figure it out, -if you don't use it for a few months or years, next time you need to do something similar, you'll have to figure it all out again. +One way we do this is by reducing the vocabulary entropy: We don't want to have to think about how every specific source calls a read, or a size, or a time to pause before reads, or what format THAT particular sensor is encoding its data in, having you shuffle through documentation pages before you can figure out how to start doing the fun stuff, which happens to be the stuff that actually produces value. And, oh, once you figure it out, if you don't use it for a few months or years, next time you need to do something similar, you'll have to figure it all out again. -No. That's just a waste of time of time. Instead, we say you do that at most once. -You don't have to do it at all if the community (us) already provided you with the their-language-to-our-consistent-language adapter for the stream you want to hook into. And if it's something new, well you'll have to figure it out, but you then write the adapter once, and now you (1) can use the rest of stream2py's tools and (2) you don't have to do it again. +No. That's just a waste of time. Instead, we say you do that at most once. You don't have to do it at all if the community (us) already provided you with the their-language-to-our-consistent-language adapter for the stream you want to hook into. And if it's something new, well you'll have to figure it out, but you then write the adapter once, and now you (1) can use the rest of stream2py's tools and (2) you don't have to do it again. ## Go back in time @@ -50,38 +52,17 @@ We also address the problem of impermanence. Think of the streams that different sensors such as audio, vibration, video offer, or even "industrial" signals such as wifi, can bus data, PLC, etc. They happen, and they're gone. Sure, they usually have buffers, but these are typically just big enough to get the data from high frequency reads -- not enough to have the time for some more involved analysis that smart systems require. -We address this problem by +We address this problem by keeping the last `maxlen` reads of the stream in a `StreamBuffer`, so a reader that starts late, or needs a window of the recent past, can still get it. ## Multi readers -It often happens that you want to do more than one thing with a stream. -Say store it, visualize it in real time, and direct it to a analysis pipeline. -In order for this to be possible with no hiccups, some things need to be taken care of. -We did, so you don't have to. +It often happens that you want to do more than one thing with a stream. Say store it, visualize it in real time, and direct it to an analysis pipeline. In order for this to be possible with no hiccups, some things need to be taken care of. We did, so you don't have to. ## Timestamp correctly -In our extensive experience with people (the write code to store stream data), -we've noticed that many engineers, when faced with the task to timestamp the segments of -stream that they're saving, follow a design pattern that goes like this -(a) get the stream data -(b) ask the system what date/time it is -(c) use that (and perhaps, just to make even more likely for the timestamp to be interpreted -incorrectly, call it the "offset_date") - -The problem with this design pattern is that it's all pattern and no design. -It is **not** the timestamp of the beginning of the segment: -That time happened **after** the **end** of the event of the end of the segment occurred, -and even more so, **after** the system that will timestamp and store. -Further, there is a lot of wiggle room in the delay accumulated between the actual event, -and the moment we ask the system what time it is. -Sometimes it doesn't matter, but sometimes it does: -For example, if we want to align with some other timestamped data, -or use these timestamps to determine if there's gaps or overlaps between the segments -we've acquired. - -Point is, stream2py will give you the tools to tackle that problem properly. -It does so by having the stream2py buffers mentioned above keep data flow statistics that readers can then use to -more precisely timestamp what they read. - -[Documentation here](https://i2mint.github.io/stream2py/index.html). +In our extensive experience with people (who write code to store stream data), we've noticed that many engineers, when faced with the task to timestamp the segments of stream that they're saving, follow a design pattern that goes like this (a) get the stream data (b) ask the system what date/time it is (c) use that (and perhaps, just to make it even more likely for the timestamp to be interpreted incorrectly, call it the "offset_date") + +The problem with this design pattern is that it's all pattern and no design. It is **not** the timestamp of the beginning of the segment: That time happened **after** the **end** of the event of the end of the segment occurred, and even more so, **after** the system that will timestamp and store. Further, there is a lot of wiggle room in the delay accumulated between the actual event, and the moment we ask the system what time it is. Sometimes it doesn't matter, but sometimes it does: For example, if we want to align with some other timestamped data, or use these timestamps to determine if there's gaps or overlaps between the segments we've acquired. + +Point is, stream2py will give you the tools to tackle that problem properly. It does so by having the stream2py buffers mentioned above keep data flow statistics that readers can then use to more precisely timestamp what they read. + From 032dd949ec4c206a525b9fcf976b26a81be14058 Mon Sep 17 00:00:00 2001 From: Thor Whalen <1906276+thorwhalen@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:22:47 +0100 Subject: [PATCH 2/2] README: position vs queue.Queue, built-in HTTP source listed, tighter plugin intro --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5561e29..78661b6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # stream2py -Bring data streams to Python, with ease. Wrap a source (a microphone, a sensor, a socket, a keyboard) once, and stream2py buffers it in a background thread and hands out as many independent readers as you need, so you can store, plot and analyse the same stream at the same time without losing data. +Bring data streams to Python, with ease. Wrap a source (a microphone, a sensor, a socket, a keyboard) once, and stream2py buffers it in a background thread and hands out as many independent readers as you need, so you can store, plot and analyse the same stream at the same time without losing data. Compared with a hand-rolled thread feeding a `queue.Queue`, where a second consumer steals the first one's items, each reader here has its own cursor over a shared buffer. ``` pip install stream2py @@ -23,13 +23,10 @@ with StreamBuffer(source_reader=source, maxlen=100) as buffer: A real source is any object with `open`, `read`, `close` and a `key` to order reads by (the `stream2py.protocols.Source` protocol; plugins below ship ready-made ones), or, for quick cases, three functions given to `stream2py.simply.mk_stream_buffer`. -stream2py is part of the i2i tools ("idea to implementation"): `dol` and `py2store` handle the storage concern, `py2http`, `front` and others expose functions to the world, and stream2py handles stream acquisition. - ## Plugins -`stream2py` has the core functionality, and is completely dependency-free (only builtin python). - -That said, to work with specific stream sources, you can install plugins that will allow you to work with them. At the time of writing this we have: +Plugins wrap specific sources: +- HTTP responses: built in, `stream2py.sources.http.HTTPResponseReader` - Audio: [audiostream2py](https://github.com/i2mint/audiostream2py) - Keyboard inputs: [keyboardstream2py](https://github.com/i2mint/keyboardstream2py) - PLC (Programmable Logic Controller): [plcstream2py](https://github.com/i2mint/plcstream2py) @@ -38,6 +35,8 @@ That said, to work with specific stream sources, you can install plugins that wi - Flask: [flaskstream2py](https://github.com/i2mint/flaskstream2py) +stream2py is part of the i2i tools ("idea to implementation"): `dol` handles the storage concern, `py2http`, `front` and others expose functions to the world, and stream2py handles stream acquisition. + # What `stream2py` is for ## Reduce vocabulary entropy