aioplus.abatched¶
- aioplus.abatched(aiterable, /, *, n, strict=False)¶
Batch data from the aiterable into tuples of length
n.- Parameters:
aiterable (AsyncIterable of T) – An asynchronous iterable of elements to be grouped into batches.
n (int) – The batch size. Each tuple will contain up to
nelements.strict (bool, default False) – If
True, raises aValueErrorif the total number of objects is not divisible byn. IfFalse, the last batch may be shorter thann.
- Returns:
An asynchronous iterable yielding tuples of at most
nelements.- Return type:
AsyncIterable of tuple[T, …]
Examples
>>> import asyncio >>> >>> from aioplus import abatched, arange >>> >>> async def main() -> None: >>> '''Run the program.''' >>> async for batch in abatched(arange(23), n=4): >>> print(batch) >>> >>> if __name__ == '__main__': >>> asyncio.run(main())
Notes
The final batch may be shorter than
n, unlessstrictis set toTrue.
See also