instancelib.utils.func module
- instancelib.utils.func.all_equal(iterable)[source]
Checks if all the elements of an iterable are the same
- 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
- 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_zippedThis 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_noneThis function works similarly, but here the iterable is not in zipped form.
- 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.
- instancelib.utils.func.intersection(*sets)[source]
Return the intersection of all sets in the parameters
- 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.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.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.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).