Introducing aiostream
aiostream is a powerful Python library designed to facilitate asynchronous iteration by offering generator-based operators. It is a versatile tool that allows developers to build asynchronous data processing pipelines akin to the well-known itertools
module, albeit with some differences tailored for asynchronous programming.
Key Features
aiostream enriches asynchronous iterables with its stream interface, providing numerous enhancements such as:
- Operator Pipelining: You can pass data through multiple stream operations using the pipe symbol
|
. This makes it easy to chain operations in a clean and readable manner. - Repeatability: Each iteration of a stream creates a different iterator, allowing for repeated processing without duplication concerns.
- Safe Iteration Context: By utilizing
async with
and thestream
method, aiostream ensures resource management is safe and efficient. - Simplified Execution: Retrieve the last element from a stream using
await
, simplifying asynchronous operations. - Slicing and Indexing: Access specific parts of streams using convenient square brackets
[]
. - Concatenation: Combine streams effortlessly using the addition symbol
+
.
Stream Operators
aiostream offers a variety of stream operators, categorized into seven distinct groups:
-
Creation: Functions like
iterate
,preserve
, andjust
help initiate streams in various ways, such as creating a stream of repeated elements or generating elements on the fly withcall
. -
Transformation: Transform data within a stream using operators like
map
,enumerate
, andcycle
. -
Selection: Select specific elements with operators such as
take
,filter
, anduntil
. -
Combination: Merge multiple streams into one using
zip
,merge
, orchain
functionalities. -
Aggregation: Aggregate data with tools like
accumulate
andreduce
. -
Advanced: Advanced data manipulation with
concat
,flatten
, and mapping techniques likeconcatmap
. -
Timing: Time-related operations are handled with
spaceout
,timeout
, anddelay
. -
Miscellaneous: Additional functionalities such as
action
andprint
for various purposes.
Demonstration
A simple demonstration of aiostream in action:
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()
Installation
Installing aiostream is simple and can be done from PyPI, allowing easy integration into Python projects as the aiostream package
.
Contact
Vincent Michel, the maintainer, can be reached at [email protected] for further inquiries or contributions.
For more examples and a thorough exploration of aiostream's capabilities, the official aiostream documentation provides extensive resources and guidance.