Batch processing with progress tracking and error handling.
Project description
philiprehberger-batch-iter
Batch processing with progress tracking and error handling.
Installation
pip install philiprehberger-batch-iter
Usage
from philiprehberger_batch_iter import batch, batch_map, collect_errors
# Split any iterable into fixed-size batches
for chunk in batch(range(10), size=3):
print(chunk)
# [0, 1, 2]
# [3, 4, 5]
# [6, 7, 8]
# [9]
# Enable progress output to stderr
for chunk in batch(range(100), size=25, progress=True):
process(chunk)
# batch 1: 25 items
# batch 2: 25 items
# ...
Batch map
from philiprehberger_batch_iter import batch_map
# Process items in batches and collect flattened results
results = batch_map(range(10), size=3, fn=lambda chunk: [x * 2 for x in chunk])
print(results)
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Error collection
from philiprehberger_batch_iter import collect_errors
def process_batch(items):
for item in items:
if item < 0:
raise ValueError(f"negative value: {item}")
result = collect_errors([1, 2, -3, 4, 5, -6], size=2, fn=process_batch)
print(result.processed) # 6
print(len(result.errors)) # 2
print(result.duration_ms) # 0.12
Async batching
from philiprehberger_batch_iter import batch_async
async def process():
async for chunk in batch_async(async_data_source(), size=50):
await handle(chunk)
Async batch map
from philiprehberger_batch_iter import batch_async_map
async def upload(chunk):
return await api.upload_many(chunk)
results = await batch_async_map(items_aiter, size=100, fn=upload)
API
| Function / Class | Description |
|---|---|
batch(iterable, size, progress=False) |
Yield fixed-size batches from an iterable |
batch_map(iterable, size, fn) |
Process batches with fn and return a flat result list |
batch_async(async_iterable, size) |
Async generator yielding fixed-size batches |
batch_async_map(async_iterable, size, fn) |
Async counterpart to batch_map, awaits each batch |
collect_errors(iterable, size, fn) |
Process batches and collect errors into a result |
BatchResult |
Dataclass with processed, errors, duration_ms |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_batch_iter-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_batch_iter-0.3.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34bbf410ccdfafdc6235980eebb991fb69d9d42f80f02451544c93f18b0ae595
|
|
| MD5 |
8d02bbf129a05059e77713000ec303dc
|
|
| BLAKE2b-256 |
5a3666bf600383eaf8af49ab61bd4953d13d20e5a167f814e719c20818901617
|
File details
Details for the file philiprehberger_batch_iter-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_batch_iter-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33ac34e6ef2509154f0170057d0877669cf6aa7a8740881bcdb8e36dfc2067e6
|
|
| MD5 |
99c673e8b291df8594538fe6be2f5020
|
|
| BLAKE2b-256 |
ac291016cdfe4c98ed8f060373a522a9b26fe69a83f7a69f71f5978a3125bc58
|