snakeoil.sequences module

sequence related operations and classes

class snakeoil.sequences.ChainedLists(*lists)[source]

Bases: object

Given a set of sequences, this will act as a proxy to them without collapsing them into a single list.

This is primarily useful when you’re dealing in large sets (or custom sequence objects), and do not want to collapse them into one sequence- but you still want to be able to access them as if they were one sequence.

Note that while you can add more lists onto this, you cannot directly change the underlying lists through this class.

>>> from snakeoil.sequences import ChainedLists
>>> l1, l2 = [0, 1, 2, 3], [4,5,6]
>>> cl = ChainedLists(l1, l2)
>>> print(cl[3])
3
>>> print(cl[4])
4
>>> print(cl[0])
0
>>> assert 4 in cl
>>> print(len(cl))
7
>>> cl[0] = 9
Traceback (most recent call last):
    ...
TypeError: not mutable
append(item)[source]

Append object to the end of the list.

extend(items)[source]

Extend list by appending elements from the iterable.

snakeoil.sequences.iflatten_func(l: Iterable, skip_func: Callable[[Any], bool]) Iterable[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(l: ~typing.Iterable, skip_flattening: ~typing.Iterable[~typing.Type] = (<class 'str'>, <class 'bytes'>)) Iterable[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)[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, stream, key=None)[source]

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_negations(iterable, func=<class 'str'>)[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.

snakeoil.sequences.stable_unique(iterable)[source]

Given a sequence, return a list of the unique items while preserving ordering.

For performance reasons, only use this if you really do need to preserve the ordering.

snakeoil.sequences.unstable_unique(sequence)[source]

Given a sequence, return a list of the unique items without preserving ordering.