Skip to main content

Batch processing with progress tracking and error handling.

Project description

philiprehberger-batch-iter

Tests PyPI version Last updated

Batch processing with progress tracking and error handling.

Installation

pip install philiprehberger-batch-iter

Usage

from philiprehberger_batch_iter import batch, 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
# ...

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)

API

Function / Class Description
batch(iterable, size, progress=False) Yield fixed-size batches from an iterable
batch_async(async_iterable, size) Async generator yielding fixed-size batches
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:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_batch_iter-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_batch_iter-0.1.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_batch_iter-0.1.2.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_batch_iter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b06519540a315c54c0a831c90b15ae13729a40314acf552cba46c0cd10162a57
MD5 cdf1715d65f6e22ab83d961fe8920f85
BLAKE2b-256 baadf2776869f15da0bc2c67a862a120deb6620f65f4762343e95b75eef87c81

See more details on using hashes here.

File details

Details for the file philiprehberger_batch_iter-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_batch_iter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 55d0e879cc5f817ef8293647c7cb5f67dc9c9a1b84ce06746b42f2745f528be9
MD5 fcc0bf85efa041e4a040c42392201e06
BLAKE2b-256 0512e0fd92a3d4404a37663c053e4cd030c4b469d9f04f116aff39587ee29ffa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page