asyncutils.iterclasses

Object-oriented (async) iteration helpers.

Classes

abucket

Async version of more_itertools.bucket.

achain

Async version of itertools.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.types.SupportsIteration[T],
key: collections.abc.Callable[[T], R],
validator: collections.abc.Callable[[R], bool],
)[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__() collections.abc.AsyncGenerator[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, /) collections.abc.AsyncGenerator[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 itertools.chain that takes async or sync iterables.

Construct an achain from the (async) iterables.

__aiter__() collections.abc.AsyncGenerator[T][source]

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

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

Construct an achain from it_of_its, an (async) iterable of (async) iterables to chain.

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.types.SupportsIteration[T])

Bases: asyncutils._internal.helpers.LoopMixinBase

Async version of more_itertools.peekable.

Wraps an (async) iterable in an asynchronous iterator and sequence APIs, supporting lookahead and prependage.

__aiter__() Self[source]

Return the instance itself.

async __anext__() T[source]

Return the next item, advancing the iterable.

async __getitem__(idx: asyncutils._internal.types.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.