Skip to content

SyncIter

class SyncIter

Bases: Generic[_T]

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

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 :return: iterable

None

all()

Checks whether all elements of this iterable are true

Returns:

Type Description
bool

True if all elements is true else False

any()

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)

Append at the position in to the iterable

Returns:

Type Description
SyncIter[_T]

iterable

append_left(item)

Append an item to left of the iterable (start)

Returns:

Type Description
SyncIter[_T]

iterable

append_right(item)

Append an item to right of the iterable (end)

Returns:

Type Description
SyncIter[_T]

iterable

batches(batch_size)

Create iterable of tuples whose length = batch_size

Returns:

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

iterable of tuples whose length = batch_size

chain(*iterables)

Chain with other iterables

Returns:

Type Description
SyncIter[_T]

iterable

contains(item)

Return True if the iterable contains item

Returns:

Type Description
bool

bool

count()

Return count of items in iterator

Returns:

Type Description
int

count of items

empty() classmethod

Create empty iterable

Returns:

Type Description
SyncIter[_T]

empty iterable

enumerate(start=0)

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 count

0

Returns:

Type Description
SyncIter[tuple[int, _T]]

interator of tuple[count, item]

first_where(func, default=_EMPTY)

Find first item for which the conditional is satisfied

Parameters:

Name Type Description Default
func _ConditionFunc

condition function

required
default _DefaultT

default value :return: item

_EMPTY

Raises:

Type Description
ValueError

the item was not found and default was not provided

flatten()

Return an iterator that flattens one level of nesting

Returns:

Type Description
SyncIter[_T]

iterable of flattened items

Raises:

Type Description
TypeError

if an encountered item is not an Iterable

is_empty()

Return True if the iterable is empty

Returns:

Type Description
bool

bool

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

Return slice from the iterable

Returns:

Type Description
SyncIter[_T]

iterable

item_at(index)

Return item at index

Returns:

Type Description
_T

item

last()

Return the last item

Returns:

Type Description
_T

last item

last_where(func, default=_EMPTY)

Find last 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)

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

Returns:

Type Description
SyncIter[_R]

async iterable

mark_first()

Mark first item.

Returns:

Type Description
SyncIter[tuple[_T, bool]]

Yields: tuple[item, is_first]

mark_first_last()

Mark first and last item

Returns:

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

Yields: tuple[item, is_first, is_last]

mark_last()

Mark last item

Returns:

Type Description
SyncIter[tuple[_T, bool]]

Yields: tuple[item, is_last]

max(key=None, default=_EMPTY)

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)

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()

Returns the next item

Returns:

Type Description
_T

first item

pairwise()

Return an iterable of overlapping pairs

Returns:

Type Description
SyncIter[tuple[_T, _T]]

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

reduce(func, initial=_EMPTY)

Apply the func 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 | _DefaultT

reduced value

Raises:

Type Description
ValueError

if initial is not provided and iterable is empty

skip(count)

Skip 'count' items from iterator

Returns:

Type Description
SyncIter[_T]

async iterable

skip_where(func)

Skip elements where conditional is satisfied

Returns:

Type Description
SyncIter[_T]

sync iterable

skip_while(func)

Skips leading elements while conditional is satisfied

Returns:

Type Description
SyncIter[_T]

sync iterable

take(count)

Take 'count' items from iterator

Returns:

Type Description
SyncIter[_T]

iterable that contains 'count' items

take_while(func)

Take items while the conditional is satisfied

Returns:

Type Description
SyncIter[_T]

iterable

to_list()

Convert to list

Returns:

Type Description
list[_T]

list of items

to_set()

Convert to set

Returns:

Type Description
set[_T]

set of items

to_tuple()

Convert to tuple

Returns:

Type Description
tuple[_T, ...]

tuple of items

where(func)

Filter items by condition

Returns:

Type Description
SyncIter[_T]

iterable

zip(*iterables, strict=False)

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
SyncIter[tuple[_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)

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
SyncIter[tuple[_T | _R, ...]]

iterable

Support Operators and Build-in func

len()

Example

len(SyncIter(range(5)))
>>> 5

in

Example

2 in SyncIter(range(5))
>>> True

Slice

Example

SyncIter(range(5))[2:]

Get item by index

Example

SyncIter(range(5))[2]
>>> 2

async_iter

Convert result of the func to SyncIter

Usage:

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

Parameters:

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

function that returns iterable

required

Returns:

Type Description
Callable[_P, SyncIter[_T]]

new function