Skip to content

AsyncIter

class AsyncIter

Bases: Generic[_T]

accumulate(func=operator.add, initial=None) async

Return series of accumulated sums (by default).

Parameters:

Name Type Description Default
func _BinaryFunc

func[accumulated value, next value], by default operator.add()

add
initial _T | None

initial value of series

None

Returns:

Type Description
AsyncIter[_R]

iterable

all() async

Checks whether all elements of this iterable are true

Returns:

Type Description
bool

True if all elements is true else False

any() async

Checks whether any element of this iterable is true

Returns:

Type Description
bool

True if any elements are true else False

append_at(index, item) async

Append at the position in to the iterable

Returns:

Type Description
AsyncIter[_T]

iterable

append_left(item) async

Append an item to left of the iterable (start)

Returns:

Type Description
AsyncIter[_T]

iterable

append_right(item) async

Append an item to right of the iterable (end)

Returns:

Type Description
AsyncIter[_T]

iterable

batches(batch_size) async

Create iterator of tuples whose length = batch_size

Returns:

Type Description
AsyncIter[tuple[_T, ...]]

iterator of tuples whose length = batch_size

chain(*iterables) async

Chain with other iterables

Returns:

Type Description
AsyncIter[_T]

iterable

contains(item) async

Return True if the iterable contains item

Returns:

Type Description
bool

True if the iterable contains item

count() async

Return count of items in iterator

Returns:

Type Description
int

count of items

empty() classmethod

Create empty iterable

Returns:

Type Description
AsyncIter[_T]

empty iterable

enumerate(start=0) async

Returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over self.

Parameters:

Name Type Description Default
start int

start of index

0

Returns:

Type Description
AsyncIter[tuple[int, _T]]

interator of tuple[index, item]

first_where(func, default=_EMPTY) async

Find first item for which the conditional is satisfied

Parameters:

Name Type Description Default
func _ConditionFunc

condition function

required
default _DefaultT

default value

_EMPTY

Returns:

Type Description
_T | _DefaultT

item

Raises:

Type Description
ValueError

the item was not found and default was not provided

flatten() async

Return an iterator that flattens one level of nesting

Returns:

Type Description
AsyncIter[_T]

async iterable of flattened items

Raises:

Type Description
TypeError

if an encountered item is neither an Iterable nor an AsyncIterable

from_sync(it) async classmethod

Create from sync iterable

Parameters:

Name Type Description Default
it Iterable[_T]

Iterable[_T], iterable

required

Returns:

Type Description
AsyncIter[_T]

async iterable

is_empty() async

Return True if iterable is empty

Returns:

Type Description
bool

Return True if iterable is empty

islice(start=0, stop=None, step=1) async

Return slice from the iterable

Returns:

Type Description
AsyncIter[_T]

iterable

item_at(index) async

Return item at index

Returns:

Type Description
_T

item

last() async

Returns the last item

Returns:

Type Description
_T

last item

last_where(func, default=_EMPTY) async

Find first item for which the conditional is satisfied

Parameters:

Name Type Description Default
func _ConditionFunc

condition function

required
default _DefaultT

default value

_EMPTY

Returns:

Type Description
_T | _DefaultT

item

Raises:

Type Description
ValueError

the item was not found and default was not provided

map(func) async

Return an iterator that applies function to every item of iterable, yielding the results

Returns:

Type Description
AsyncIter[_R]

iterable

mark_first() async

Mark first item

Returns:

Type Description
AsyncIter[tuple[_T, bool]]

Yields: tuple[item, is_first]

mark_first_last() async

Mark first and last item

Returns:

Type Description
AsyncIter[tuple[_T, bool, bool]]

Yields: tuple[item, is_first, is_last]

mark_last() async

Mark last item

Returns:

Type Description
AsyncIter[tuple[_T, bool]]

Yields: tuple[item, is_last]

max(key=None, default=_EMPTY) async

Return the biggest item.

Parameters:

Name Type Description Default
key _KeyFunc | None

the result of the function will be used to compare the elements.

None
default _DefaultT

default value in case iterable is empty

_EMPTY

Returns:

Type Description
_T | _DefaultT

the biggest item

Raises:

Type Description
ValueError

when iterable is empty and default value is not provided

min(key=None, default=_EMPTY) async

Return the smallest item.

Parameters:

Name Type Description Default
key _KeyFunc | None

the result of the function will be used to compare the elements.

None
default _DefaultT

default value in case iterable is empty

_EMPTY

Returns:

Type Description
_T | _DefaultT

the smallest item

Raises:

Type Description
ValueError

when iterable is empty and default value is not provided

next() async

Returns the first item

Returns:

Type Description
_T

first item

pairwise() async

Return an iterable of overlapping pairs

Returns:

Type Description
AsyncIter[tuple[_T, _T]]

tuple[item_0, item_1], tuple[item_1, item_2], ...

reduce(func, initial=_EMPTY) async

Apply a function of two arguments cumulatively to the items of an iterable, from left to right, to reduce the iterable to a single value.

Parameters:

Name Type Description Default
func _BinaryFunc

func[accumulated value, next item]

required
initial _T

initial value of iterable. Serves like default value if iterable is empty.

_EMPTY

Returns:

Type Description
_T | _R

reduced value

Raises:

Type Description
ValueError

if initial is not provided and iterable is empty

skip(count) async

Skip 'count' items from iterator

Returns:

Type Description
AsyncIter[_T]

iterable without first 'count' items

skip_where(func) async

Skip elements where conditional is satisfied

Returns:

Type Description
AsyncIter

async iterable

skip_while(func) async

Skips leading elements while conditional is satisfied

Returns:

Type Description
AsyncIter[_T]

iterable

take(limit) async

Take 'count' items from iterator

Returns:

Type Description
AsyncIter[_T]

iterable that contains 'count' items

take_while(func) async

Take items while the conditional is satisfied

Returns:

Type Description
AsyncIter[_T]

iterable

to_list() async

Convert to list

Returns:

Type Description
list[_T]

list of items

to_set() async

Convert to set

Returns:

Type Description
set[_T]

set of items

to_tuple() async

Convert to tuple

Returns:

Type Description
tuple[_T, ...]

tuple of items

where(func) async

Filter item by condition

Returns:

Type Description
AsyncIter[_T]

iterable

zip(*iterables, strict=False) async

The zip object yields n-length tuples, where n is the number of iterables passed as positional arguments to zip(). The i-th element in every tuple comes from the i-th iterable argument to zip(). This continues until the shortest argument is exhausted.

Returns:

Type Description
AsyncIter[list[_T]]

iterable

Raises:

Type Description
ValueError

when strict is true and one of the arguments is exhausted before the others

zip_longest(*iterables, fillvalue=None) async

The zip object yields n-length tuples, where n is the number of iterables passed as positional arguments to zip(). The i-th element in every tuple comes from the i-th iterable argument to zip(). This continues until the longest argument is exhausted.

Parameters:

Name Type Description Default
fillvalue _R

when the shorter iterables are exhausted, the fillvalue is substituted in their place

None

Returns:

Type Description
AsyncIter[list[_T | _R]]

iterable

Support Operators and Build-in func

Slice

Example

AsyncIter.from_sync(range(5))[2:]

Get item by index

Example

await AsyncIter.from_sync(range(5))[2]
>>> 2

async_iter

Convert result of the func to AsyncIter

Usage:

@async_iter
def my_generator() -> AsyncIter[int]:
    for i in range(10):
        yield i

Parameters:

Name Type Description Default
func Callable[_P, AsyncIterable[_T]]

function that returns async iterable

required

Returns:

Type Description
Callable[_P, AsyncIter[_T]]

new function