asyncutils.iterclasses

Object-oriented (async) iteration helpers.

Classes

ABucket

Async version of more_itertools.bucket().

AChain

Async version of chain() that takes async or sync iterables.

APeekable

Async version of more_itertools.peekable.

Module Contents

class asyncutils.iterclasses.ABucket[T, R](
it: asyncutils._internal.prots.SupportsIteration[T],
key: collections.abc.Callable[[T], R],
validator: collections.abc.Callable[[R], bool] | None = ...,
)[source]

Bases: asyncutils.mixins.LoopContextMixin

Async version of more_itertools.bucket().

Divide items from the (async) iterable it into child generators according to a key function.

__aiter__() types.AsyncGeneratorType[T][source]

Yield the keys of all buckets. When this async generator is exhausted, the original iterable is fully consumed. Unlike the sync version, an exhausted bucket has no keys.

__getitem__(key: R, /) types.AsyncGeneratorType[T][source]

Return an async generator of the items in the original iterable for which the key function gives this key.

async contains(key: R, /) bool[source]

If validator returns False for key, return False immediately. Otherwise, advance the iterable and store items until an item mapping to key under the key function is seen.

class asyncutils.iterclasses.AChain[T][source]

Async version of chain() that takes async or sync iterables.

Construct an AChain from the (async) iterables.

__aiter__() types.AsyncGeneratorType[T][source]

Yield items from the first iterable until exhausted, then start on the second, etc.

classmethod from_iterable(
it_of_its: asyncutils._internal.prots.SupportsIteration[asyncutils._internal.prots.SupportsIteration[T]],
) Self[source]

Construct an AChain from it_of_its, an (async) iterable of (async) iterables to chain. If some iterables are chains themselves, their internal iterable of iterables are flattened into that of the returned AChain, which may cause unexpected behaviour when iterating through these sources themselves.

Tip

Since the outer iterable is advanced on demand, a possible use case would be to combine chunks of items from different sources as they arrive.

class asyncutils.iterclasses.APeekable[T][source]
class asyncutils.iterclasses.APeekable(it: asyncutils._internal.prots.SupportsIteration[T])

Bases: asyncutils._internal.helpers.LoopMixinBase

Async version of more_itertools.peekable.

Wrap an (async) iterable in an asynchronous iterator and sequence APIs, supporting lookahead and prepending.

__aiter__() Self[source]

Return the instance itself.

async __anext__() T[source]

Return the next item, advancing the iterable.

async __getitem__(idx: asyncutils._internal.prots.ValidSlice, /) tuple[T, Ellipsis][source]
async __getitem__(idx: SupportsIndex, /) T

Slice or index access. Must be awaited.

async can_peek() bool[source]

Check whether any items are left in the underlying iterable without advancing it.

async peek(default: T = ...) T[source]

Return the next item of the underlying iterable without advancing it, or default if the items have run out.

prepend(/, *items: T) None[source]

Yield the prepended items in the order passed in first instead of advancing the underlying iterable.