Comments (5)
Just thought I'd share some ideas for streams that would be useful to me.
Great, thanks!
A ziplast stream
That would be the equivalent of ReactiveX combineLatest right?
Here's a possible implementation:
@operator(pipable=True)
def ziplatest(*sources):
n = len(sources)
# Add source index to the items
new_sources = [
stream.map(source, lambda x, i=i: {i: x})
for i, source in enumerate(sources)]
# Merge the sources
merged = stream.merge.raw(*new_sources)
# Accumulate the current state in a dict
accumulated = stream.accumulate.raw(
merged, lambda x, e: {**x, **e})
# Convert the state dict to a tuple
return stream.map.raw(
accumulated, lambda x: tuple(map(x.get, range(n))))
And an example:
async def main():
xs = stream.count(interval=2) | pipe.delay(2)
ys = stream.count(interval=5) | pipe.delay(5)
zs = xs | ziplatest.pipe(ys)
ps = zs | pipe.print()
await ps
With the corresponding output:
(0, None)
(1, None)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(4, 1)
(5, 1)
(6, 1)
(6, 2)
(7, 2)
[...]
I guess extra keywords arguments could also be provided, either to disable the producing of incomplete items, or to define a custom default value.
What do you think?
from aiostream.
Awesome! That is exactly what I need.
from aiostream.
Great! I don't really have the time to write the docs and tests for this feature at the moment, so feel free to submit a PR if you're interested. Otherwise, I'll take care of it later.
from aiostream.
Np, thanks for the effort. I really like this library and async generator functions is a pretty nice new python feature. It really cleans up a lot of my previous async context manager code in the project I am currently working at. You get a feeling that async/await is still a pretty young feature in python and there are definitely a few pitfalls to dodge here and there.
from aiostream.
Hi @andersea,
I just added the ziplatest
operator (see PR #22), it will become available with version 0.3.1.
Thanks for the suggestion :)
from aiostream.
Related Issues (20)
- Incompatibility with mypy 1.7 and later
- Docstring and prototype not properly shown with pylance HOT 1
- New operators
- No easy to see installation instructions in the docs or README HOT 1
- merge causes anext(): asynchronous generator is already running Exception HOT 2
- question: how to use chain HOT 8
- Feature suggestion: `strict` parameter for `zip` HOT 3
- How to duplicate streams properly? HOT 5
- add support python3.10 HOT 1
- Please upload a wheel release to pypi HOT 2
- `stream.list` implementation is x10 slower than using plain Python built-in functionality (list comprehension) HOT 2
- Idea: Timeouts on chunks HOT 4
- action: support task_limit HOT 5
- Regarding `update_pipe_module` HOT 3
- Aiostream fails to import with a TypeError HOT 1
- CI test pipeline doesn't run in Python 3.12: ModuleNotFoundError: No module named 'setuptools' HOT 6
- License change HOT 3
- 0.5.0 (#84) made backwards incompatible changes HOT 6
- asyncio.Event for graceful/early termination HOT 7
- How to handle stream splitting HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiostream.