Effortlessly manage Python pipelines with lazy execution and async capabilities.
Project details
fpstreams provides a flexible framework for creating lazy Python pipelines that efficiently handle iterables and records. With bounded async concurrency and explicit resource management, it allows for optimized data processing, making it an essential tool for developers looking to streamline their workflows in Python.
fpstreams is a Python library designed for processing iterables and records through lazy pipelines, enabling bounded asynchronous concurrency and efficient resource management. This project showcases an innovative approach to handling data transformations and aggregations in Python, allowing for rich, scalable data processing with ease.
flow() for synchronous pipelines, or aflow() for asynchronous processing that respects concurrency limits.finally blocks to ensure that generators and iterators are properly closed upon completion.Here's a simple example demonstrating how to use the flow() function:
from fpstreams import flow
result = flow([1, 2, 3]).filter(lambda value: value > 1).map(lambda value: value * 10).to_list()
assert result == [20, 30]
For asynchronous processing, use the aflow() function:
import asyncio
from fpstreams import aflow
async def fetch(value: int) -> int:
await asyncio.sleep(0.01)
return value * 10
async def main() -> None:
result = await (
aflow([1, 2, 3, 4])
.map_async(fetch, concurrency=2, ordered=True)
.filter(lambda value: value >= 20)
.to_list()
)
print(result)
# Output: [20, 30, 40]
asyncio.run(main())
aggregate() function along with built-in aggregation functions such as count(), sum(), and mean().group_by() and with_columns().Using flow() for record aggregation:
from fpstreams import agg, col, flow
orders = [
{"region": "eu", "status": "paid", "price": 12, "quantity": 2},
{"region": "us", "status": "paid", "price": 20, "quantity": 1},
{"region": "eu", "status": "cancelled", "price": 99, "quantity": 1},
{"region": "eu", "status": "paid", "price": 8, "quantity": 3},
]
revenue = (
flow(orders)
.filter(col("status") == "paid")
.with_columns(revenue=col("price") * col("quantity"))
.group_by("region")
.aggregate(
orders=agg.count(),
revenue=agg.sum("revenue"),
)
.sort_by("region")
.to_list()
)
assert revenue == [
{"region": "eu", "orders": 2, "revenue": 48},
{"region": "us", "orders": 1, "revenue": 20},
]
In summary, fpstreams offers a robust and efficient way to handle data processing in Python, enhancing productivity with its lazy evaluation and resource-efficient operations. Explore the documentation to learn more about this powerful library.
Comments
0Start the conversation
Share the first comment.