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 n elements.

  • strict (bool, default False) – If True, raises a ValueError if the total number of objects is not divisible by n. If False, the last batch may be shorter than n.

Returns:

An asynchronous iterable yielding tuples of at most n elements.

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, unless strict is set to True.