instancelib.utils.func module

instancelib.utils.func.all_equal(iterable)[source]

Checks if all the elements of an iterable are the same

Parameters:

iterable (Iterable[Any]) – An iterable of generic type

Returns:

True if all elements are the same

Return type:

bool

instancelib.utils.func.all_subsets(fset, n_min, n_max)[source]

Find all subsets of fset with size n_min to and including n_max

Parameters:
  • fset (FrozenSet[_T]) – The input set

  • n_min (int) – The mininum size of the subsets

  • n_max (int) – The maximum size of the subsets

Returns:

A set of subsets

Return type:

FrozenSet[FrozenSet[_T]]

instancelib.utils.func.expandgrid(**itrs)[source]

Port of the expand.grid function from R

Parameters:

itrs (Keyword variables with an iterable as argument) –

Returns:

A dictionary with as values all possible combinations in a gridlike ordering

Return type:

Dict[str, List[_T]]

instancelib.utils.func.filter_snd_none(fst_iter, snd_iter)[source]

Filter two iterables of equal length for pairs where the second element is not None

Parameters:
Returns:

A tuple of two lists of equal length based on all pairs based on index where the element in the second list was not None:

  • A list containing all eligble elements from fst_iter

  • A list containing all eligble elements from snd_iter

Return type:

Tuple[Sequence[_T], Sequence[_U]]

Examples

Usage:

>>> a = [1,2,3,4,5,6,7,8,9,10]
>>> b = ["a", None, "c", "d", "e", "f", "g", None,"i", "j"]
>>> a_filtered, b_filtered = filter_snd_none(a,b)
>>> print(a_filtered)
>>> # [1, 3, 4, 5, 6, 7, 9, 10]
>>> print(b_filtered)
>>> # ["a", "c", "d", "e", "f", "g", "i", "j"]

See also

filter_snd_none_zipped

This function works similarly, but here the iterable is in zipped form, i.e., a list of tuples.

instancelib.utils.func.filter_snd_none_zipped(iter)[source]

Filter a list of tuples where the second element is not None

Parameters:

iter (Iterable[Tuple[_T, Optional[_U]]]) – A list of tuples where the second element may be None

Returns:

A tuple of two lists of equal length based on all tuples where the second element was not None:

  • A list of elements with type _T

  • A list of elements with type _U

Return type:

Tuple[Sequence[_T], Sequence[_U]]

See also

filter_snd_none

This function works similarly, but here the iterable is not in zipped form.

instancelib.utils.func.find_subsets(s, n)[source]
Parameters:
Return type:

FrozenSet[FrozenSet[TypeVar(_T)]]

instancelib.utils.func.flatten_dicts(*dicts)[source]

Recursive function that combines a list of dictionaries into a single dictionary. When a key exists multiple times, the last one is preserved.

Parameters:

dicts (Mapping[TypeVar(_T), TypeVar(_U)]) – A sequence dicts that should be combined

Returns:

A single dictionary combining all given dictionaries.

Return type:

Dict[_T, _U]

instancelib.utils.func.fst(tup)[source]
Parameters:

tup (Tuple[TypeVar(_T), Any]) –

Return type:

TypeVar(_T)

instancelib.utils.func.identity(x)[source]
Parameters:

x (TypeVar(_T)) –

Return type:

TypeVar(_T)

instancelib.utils.func.intersection(*sets)[source]

Return the intersection of all sets in the parameters

Parameters:

sets (FrozenSet[TypeVar(_T)]) – A sequence of frozensets that should be combined

Returns:

A frozenset that contains the intersection of all the sets

Return type:

FrozenSet[_T]

instancelib.utils.func.invert_mapping(source)[source]
Parameters:

source (Mapping[TypeVar(_T), TypeVar(_V)]) –

Return type:

Dict[TypeVar(_V), TypeVar(_T)]

instancelib.utils.func.list_unzip(iterable)[source]

Unzips an iterable of tuples of two elements, and returns a tuple of two lists, where the first contains all the first elements of the tuples and the latter the second elements

Parameters:

iterable (Iterable[Tuple[TypeVar(_T), TypeVar(_U)]]) – An iterable of tuples of indiscriminate type

Returns:

A tuple of two sequences:

  • The first contains all first elements (of type _T)

  • The second contains all second elements (of type _U)

Return type:

Tuple[Sequence[_T], Sequence[_U]]

Examples

Usage

>>> tuple_list = [(1, "a"), (2, "b"), (3, "c")]
>>> idx, abcs = list_unzip(tuple_list)
>>> print(idx)
>>> # [1, 2, 3]
>>> print(abcs)
>>> # ["a", "b", "c"]
instancelib.utils.func.list_unzip3(iterable)[source]

Unzips an iterable of tuples of three elements, and returns a tuple of three lists, where the first contains all the first elements of the tuples and the latter the second elements, etc.

Parameters:

iterable (Iterable[Tuple[TypeVar(_T), TypeVar(_U), TypeVar(_V)]]) – An iterable of tuples of indiscriminate type

Returns:

A tuple of three sequences:

  • The first contains all first elements (of type _T)

  • The second contains all second elements (of type _U)

  • The third contains all third elements (of type _V)

Return type:

Tuple[Sequence[_T], Sequence[_U], Sequence[_V]]

Examples

Usage

>>> tuple_list = [(1, "a", 20), (2, "b", 40), (3, "c", 60)]
>>> idx, abcs, ints = list_unzip3(tuple_list)
>>> print(idx)
>>> # [1, 2, 3]
>>> print(abcs)
>>> # ["a", "b", "c"]
>>> print(ints)
>>> # [20, 40, 60]
instancelib.utils.func.mapping_map(mapping, iterable)[source]
Parameters:
Return type:

Iterable[TypeVar(_V)]

instancelib.utils.func.mapsnd(func)[source]

This function converts a function with signature u -> v to a function with signature (t, u) -> (t, v).

That is, a function that is applied to the second element of a tuple and where the first element is preserved.

Parameters:

func (Callable[[TypeVar(_U)], TypeVar(_V)]) – The original function

Returns:

The new function

Return type:

Callable[[_T, _U], Tuple[_T, _V]]

Examples

Usage

>>> def plustwo(number: int) -> int:
...     return number + 2
>>> tuple_variable = (2, 3)
>>> mapped_p2 = mapsnd(plustwo)
>>> result = mapped_p2(tuple_variable)
>>> print(result)
>>> # (2, 5)
instancelib.utils.func.multisort(seq)[source]
Parameters:

seq (Sequence[Tuple[TypeVar(_T), Sequence[TypeVar(_U)]]]) –

Return type:

Sequence[Sequence[Tuple[TypeVar(_T), TypeVar(_U)]]]

instancelib.utils.func.not_in_supersets(contingency)[source]

This function filters out all values that also exist in supersets in the dictionary for the values that exist in all pairs in the mapping key sets to value sets

TODO: Make a clear description for this function

Parameters:

contingency (Dict[FrozenSet[_T], FrozenSet[_U]]) – A dictionary that maps key set to sets of values

Returns:

A dictionary that maps sets to sets of of values. The values of that belong to a key set only contain values that do not exist in a superset of the keyset.

Return type:

Dict[FrozenSet[_T], FrozenSet[_U]]

instancelib.utils.func.powerset(iterable)[source]

Calculates the powerset of an interable.

Parameters:

iterable (Iterable[_T]) – An iterable of which we want to calculate the powerset

Returns:

Returns a frozenset of frozensets containing all elements of the powerset

Return type:

FrozenSet[FrozenSet[_T]]

Examples

Usage >>> powerset([1,2,3]) >>> # frozenset({() (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)}) >>> # all elements are also frozensets

instancelib.utils.func.seq_or_map_to_map(seq_or_map)[source]
Parameters:

seq_or_map (Union[Sequence[TypeVar(_T)], Mapping[int, TypeVar(_T)]]) –

Return type:

Mapping[int, TypeVar(_T)]

instancelib.utils.func.single_or_collection(elem)[source]
Parameters:

elem (Union[str, Iterable[str]]) –

Return type:

Sequence[str]

instancelib.utils.func.sort_on(index, seq)[source]

A function that allows you to sort a sequence on the n-th element of a tuple it contains. This function orders in descending order (high to low).

Parameters:
  • index (int) – The index of the tuple element on which you desire to sort

  • seq (Sequence[Tuple[TypeVar(_T), TypeVar(_U)]]) – The sequence of tuples

Returns:

A sorted sequence of tuples

Return type:

Sequence[Tuple[_T, _U]]

instancelib.utils.func.union(*sets)[source]

Return the union of all sets in the parameters

Parameters:

sets (FrozenSet[TypeVar(_T)]) – A sequence of frozensets that should be combined

Returns:

A frozenset that contains the union of all the sets

Return type:

FrozenSet[_T]

instancelib.utils.func.unzip_chain(iterable)[source]
Parameters:

iterable (Iterable[Tuple[Sequence[TypeVar(_T)], Sequence[TypeVar(_U)]]]) –

Return type:

Tuple[Sequence[TypeVar(_T)], Sequence[TypeVar(_U)]]

instancelib.utils.func.value_map(f, mapping)[source]
Parameters:
Return type:

Mapping[TypeVar(_K), TypeVar(_U)]

instancelib.utils.func.zip_chain(iterable)[source]
Parameters:

iterable (Iterable[Tuple[Sequence[TypeVar(_T)], Sequence[TypeVar(_U)]]]) –

Return type:

Iterator[Tuple[TypeVar(_T), TypeVar(_U)]]