Generator-based operators for asynchronous iteration
Project description
Generator-based operators for asynchronous iteration
Synopsis
aiostream provides a collection of stream operators that can be combined to create asynchronous pipelines of operations.
It can be seen as an asynchronous version of itertools, although some aspects are slightly different. Essentially, all the provided operators return a unified interface called a stream. A stream is an enhanced asynchronous iterable providing the following features:
Operator pipe-lining - using pipe symbol |
Repeatability - every iteration creates a different iterator
Safe iteration context - using async with and the stream method
Simplified execution - get the last element from a stream using await
Slicing and indexing - using square brackets []
Concatenation - using addition symbol +
Stream operators
The stream operators are separated in 7 categories:
creation |
iterate, preserve, just, call, empty, throw, never, repeat, count, range |
transformation |
|
selection |
take, takelast, skip, skiplast, getitem, filter, until, takewhile, dropwhile |
combination |
|
aggregation |
|
advanced |
|
timing |
|
miscellaneous |
Demonstration
The following example demonstrates most of the streams capabilities:
import asyncio
from aiostream import stream, pipe
async def main():
# Create a counting stream with a 0.2 seconds interval
xs = stream.count(interval=0.2)
# Operators can be piped using '|'
ys = xs | pipe.map(lambda x: x**2)
# Streams can be sliced
zs = ys[1:10:2]
# Use a stream context for proper resource management
async with zs.stream() as streamer:
# Asynchronous iteration
async for z in streamer:
# Print 1, 9, 25, 49 and 81
print('->', z)
# Streams can be awaited and return the last value
print('9² = ', await zs)
# Streams can run several times
print('9² = ', await zs)
# Streams can be concatenated
one_two_three = stream.just(1) + stream.range(2, 4)
# Print [1, 2, 3]
print(await stream.list(one_two_three))
# Run main coroutine
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
More examples are available in the example section of the documentation.
Installation
You can install aiostream from PyPI as the aiostream package.
Contact
Vincent Michel: vxgmichel@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aiostream-0.6.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f455e6235d5e2ede25f8261425442f49db60602d5abb18522e780bb421b4f79e |
|
MD5 | 2526bfaca4f4f41ee5d2cbfd87e9f4bc |
|
BLAKE2b-256 | 37af9d2caa3bedb31baa8cd7e1cf92ee0cef3f61605d602a87065da57a5c51cb |