~.iterator
CacheIteratorStep
Bases: Protocol[T, U]
Implement a custom iterator pipeline step.
__call__
Return (value, skip, stop).
stop = True, skip = True -> Skip this value, then stop.
stop = True, skip = False -> Yield this value, then stop.
stop = False, skip = True -> Skip this value, continue.
stop = False, skip = False -> Yield this value, continue.
CacheIterator
CacheIterator(source: AsyncIterator[T])
Bases: Generic[T]
Asynchronous iterator for cached objects.
Create a new cache iterator from an async iterator.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The source iterator.
TYPE:
|
Note
The source iterator is shared across transformations created from this iterator. Multiple concurrent iterations are not supported.
add_step
add_step(
step: CacheIteratorStep[T, U],
) -> CacheIterator[U]
Add a custom-implemented CacheIteratorStep into the iterator's pipeline.
| PARAMETER | DESCRIPTION |
|---|---|
step
|
The iterator step to add.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[U]
|
A chain-callable async iterator over the results. |
all
async
any
async
chunk
chunk(size: int) -> CacheIterator[list[T]]
Set the amount of results to be handled in each chunk.
| PARAMETER | DESCRIPTION |
|---|---|
size
|
The amount of results.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[list[T]]
|
A chain-callable async iterator over the results. |
collect
async
collect() -> list[T]
Collect all items in the iterator into a list.
| RETURNS | DESCRIPTION |
|---|---|
list[T]
|
All iterator items. |
count
async
count() -> int
Get the total amount of items in the iterator.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The amount of items. |
enumerate
enumerate(start: int = 0) -> CacheIterator[tuple[int, T]]
Enumerate each item, yielding a (index, item) tuple for each result.
| PARAMETER | DESCRIPTION |
|---|---|
start
|
The starting index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[tuple[int, T]]
|
A chain-callable async iterator over the enumerated results. |
filter
filter(
predicate: Callable[[T], bool],
) -> CacheIterator[T]
Filter through each item in the iterator and check it against a predicate.
| PARAMETER | DESCRIPTION |
|---|---|
predicate
|
The predicate method to check against each item.
If |
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[T]
|
A chain-callable async iterator over the results. |
flat_map
flat_map(
func: Callable[[T], AsyncIterator[U]],
) -> CacheIterator[U]
Map each item to an async iterator and flatten all results into a single iterator.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
A method that takes each item and returns an async iterator of results.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[U]
|
A chain-callable async iterator over the flatten results. |
find
async
first
async
first() -> T | None
Get the first item in the iterator.
| RETURNS | DESCRIPTION |
|---|---|
T | None
|
If present, the first item. |
last
async
last() -> T | None
Get the last item in the iterator.
| RETURNS | DESCRIPTION |
|---|---|
T | None
|
If present, the last item. |
limit
limit(size: int) -> CacheIterator[T]
Limit the size of the iterator.
| PARAMETER | DESCRIPTION |
|---|---|
size
|
The size limit of the iterator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[T]
|
A chain-callable async iterator over the results. |
map
map(func: Callable[[T], U]) -> CacheIterator[U]
Map each iterator item through a mapping method.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The mapping method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[U]
|
A chain-callable async iterator over the results. |
max
async
min
async
reduce
async
reduce(func: Callable[[U, T], U], initial: U) -> U
Aggregate all iterator items into one value using a reducing method.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The reducing method.
TYPE:
|
initial
|
The initial value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
U
|
The reduced iterator items. |
skip_while
skip_while(
predicate: Callable[[T], bool],
) -> CacheIterator[T]
Skip items while the predicate returns True, then yield all remaining items.
| PARAMETER | DESCRIPTION |
|---|---|
predicate
|
The predicate to check each item against. |
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[T]
|
A chain-callable async iterator over the results. |
sort
async
sort(
*,
key: (
Callable[[T], SupportsRichComparison] | None
) = None,
reverse: bool = False
) -> list[T]
Collect all items (consuming them) and return them sorted.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
If provided, a method used to extract a comparison key from each item.
TYPE:
|
reverse
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[T]
|
All iterator items, sorted. |
take_while
take_while(
predicate: Callable[[T], bool],
) -> CacheIterator[T]
Yield items while the predicate returns True, then stop.
| PARAMETER | DESCRIPTION |
|---|---|
predicate
|
The predicate to check each item against. |
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[T]
|
A chain-callable async iterator over the results. |
unique
unique(
*, key: Callable[[T], Hashable] | None = None
) -> CacheIterator[T]
Yield only unique items, discarding duplicates.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
If provided, uniqueness is determined by the key value rather than the item itself. |
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[T]
|
A chain-callable async iterator over the unique results. |
zip
zip(
other: CacheIterator[U] | AsyncIterator[U],
) -> CacheIterator[tuple[T, U]]
Pair each item with the corresponding item from another async iterator. Stops when either iterator is exhausted.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The async iterator to zip with.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CacheIterator[tuple[T, U]]
|
A chain-callable async iterator over the paired results. |