aioplus.abatched

aioplus.abatched(aiterable, /, *, n, strict=False)

Iterate aiterable by batches of length n.

Parameters:
  • aiterable (AsyncIterable[T]) – Iterable.

  • n (int) – Batch size.

  • strict (bool, default False) – Strictness.

Returns:

Iterator.

Return type:

AsyncIterator[tuple[T, …]]

Notes

  • If strict is True and the total number of objects is not divisible by n, then raises ValueError. If False, the last batch may be shorter than n.

Examples

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