snakeoil.sequences module¶
sequence related operations and classes
- snakeoil.sequences.iflatten_func(iterable: Iterable[T] | T, skip_func: Callable[[T], bool], /) Iterable[T][source]¶
collapse [[1],2] into [1,2]
- Parameters:
skip_func – a callable that returns True when iflatten_func should descend no further
- Returns:
this generator yields each item that cannot be flattened (or is skipped due to a True result from skip_func)
- snakeoil.sequences.iflatten_instance(iterable: ~typing.Iterable[~snakeoil.sequences.T], skip_flattening: tuple[type, ...] | type = (<class 'str'>, <class 'bytes'>), /) Iterable[T | str | bytes][source]¶
collapse [[1],2] into [1,2]
- Parameters:
skip_flattening – list of classes to not descend through
- Returns:
this generator yields each item that cannot be flattened (or is skipped due to being a instance of
skip_flattening)
- snakeoil.sequences.iter_stable_unique(iterable: Iterable[T]) Iterator[T][source]¶
Given a sequence, yield unique items while preserving ordering.
For performance reasons, only use this if you really do need to preserve the ordering.
- snakeoil.sequences.predicate_split(func: Callable[[T], bool], stream: Iterable[T], /, key: Literal[None] = None)[source]¶
- snakeoil.sequences.predicate_split(func: Callable[[T2], bool], stream: Iterable[T], /, key: Callable[[T], T2] | None = None)
Given a stream and a function, split the stream into two sequences based on the results of the func for that item
- Parameters:
func – function to invoke with the item; function must return True or False
stream – iterable to split into two sequences
key – if set, a function to use to pull the attribute to inspect. Basically the same sort of trick as the key paramater for
sorted()
- Returns:
two lists, the first a list of everything that was False, the second a list of everything that was True
Example usage:
>>> from snakeoil.sequences import predicate_split >>> odd, even = predicate_split(lambda x:x % 2 == 0, range(10)) >>> assert odd == [1, 3, 5, 7, 9] >>> assert even == [0, 2, 4, 6, 8]
- snakeoil.sequences.split_elements(iterable: ~typing.Iterable[str], func: ~typing.Callable[[str], ~snakeoil.sequences.T] = <class 'str'>) BoolTernaryResults[T][source]¶
“Split an iterable into negative, neutral, and positive elements.
- Parameters:
iterable – iterable targeted for splitting
func – wrapper method to modify tokens
- Returns:
Tuple containing negative, neutral, and positive element tuples, respectively.
- snakeoil.sequences.split_negations(iterable: ~typing.Iterable[str], func: ~typing.Callable[[str], ~snakeoil.sequences.T] = <class 'str'>) BoolSplitResults[T][source]¶
Split an iterable into negative and positive elements.
- Parameters:
iterable – iterable targeted for splitting
func – wrapper method to modify tokens
- Returns:
Tuple containing negative and positive element tuples, respectively.