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

>>> aiterable = arange(23)
>>> [batch async for batch in abatched(aiterable, n=3)]
[(0, 1, 2), (3, 4, 5), ..., (18, 19, 20), (21, 22)]

Notes

  • The final batch may be shorter than n, unless strict is set to True.