les_iterables.functions module¶
Summary¶
Classes:
HeadPartitionIterator |
|
PartitionedTail |
|
TailPartitionIterator |
Functions:
elements_at |
Select elements from a sequence based on their indexes. |
empty_iterable |
|
extended_unchain |
Convert an iterable into an infinite series of lists of containing zero or one items. |
false_then_true |
A single False value followed by True values. |
generate |
|
group_by_terminator |
Group the items of of an iterable series, starting a new group after each terminator. |
indexes |
The indexes at which item occurs in a sequence. |
just |
An iterable of just one item. |
pairwise_padded |
|
partition_tail |
Lazily partition an iterable series into a head, and tail of no more than specified length. |
run_length_encode |
|
split_around |
Split an iterable series into groups around specific items. |
transform_if |
|
true_then_false |
A single True value followed by False values. |
unchain |
Reference¶
-
les_iterables.functions.
just
(item)[source]¶ An iterable of just one item.
Parameters: item – The item to be yielded. Yields: The item.
-
les_iterables.functions.
group_by_terminator
(iterable, predicate, group_factory=None)[source]¶ Group the items of of an iterable series, starting a new group after each terminator.
Each group will have as it’s last item an item from which the predicate returns True. For all preceding items in the group the predicate will return False. The last group yielded may be incomplete, without a terminator.
Parameters: - iterable – An iterable series of items to be grouped.
- predicate – A unary callable function used to detect group-terminating items from the iterable series.
- group_factory – A callable which creates a group given an sequence of items. By default, a list.
Yields: A series of groups.
-
les_iterables.functions.
split_around
(iterable, predicate, group_factory=None)[source]¶ Split an iterable series into groups around specific items.
Each item for which the predicate returns True will be in its own group.
Example:
split_around(“abcdef “, is_newline) -> [‘a’, ‘b’, ‘c’], [’ ‘], [‘d’, ‘e’, ‘f’], [’ ‘]
- Args:
iterable: An iterable series of items to be grouped.
predicate: A unary callable to detect items which should be placed in their own group.
- group_factory: A callable which creates a group given a sequence of items. By default, a
- list.
- Yields:
- A series of groups.
-
les_iterables.functions.
elements_at
(seq, indexes)[source]¶ Select elements from a sequence based on their indexes.
Parameters: - seq – The sequence from which to select elements.
- indexes – Indexes into seq indicating the selected elements.
Yields: A series of items selected from seq by indexes.
Raises: IndexError
– If one of the indexes is not valid with seq.
-
les_iterables.functions.
indexes
(seq, item)[source]¶ The indexes at which item occurs in a sequence.
Parameters: - seq – A sequence in which to search for occurrences of item.
- item – The item for which to determine indexes.
Yields: A series of indexes into seq at which item occurs.
-
les_iterables.functions.
partition_tail
(items, n)[source]¶ Lazily partition an iterable series into a head, and tail of no more than specified length.
Parameters: - items – An iterable series of items.
- n – The maximum number of items to be partitioned into the tail.
Returns: A pair of iterators, head and tail. Consuming any items from the tail iterator will cause the entire head iterator to be consumed, so typically the head iterator should be consumed before consuming any items from the tail iterator.
Example
head, tail = partition_tail(range(10), 3) for item in head:
print(item) # Prints all but the last three- for item in tail:
- print(item) # Prints the last three