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_left(item)
append_right(item)
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)
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
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)
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_last()
Mark first and last item
Returns:
Type | Description |
---|---|
SyncIter[tuple[_T, bool, bool]]
|
Yields: tuple[item, is_first, is_last] |
mark_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_where(func)
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)
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)
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 |