mpu.datastructures
Utility datastructures.
- class mpu.datastructures.EList(*args: Iterable[mpu.datastructures.T])[source]
Bases:
list
,Generic
[mpu.datastructures.T
]Enhanced List.
This class supports every operation a normal list supports. Additionally, you can call it with a list as an argument.
Examples
>>> l = EList([2, 1, 0]) >>> l[2] 0 >>> l[[2, 0]] [0, 2] >>> l[l] [0, 1, 2]
- remove_indices(indices: List[int]) mpu.datastructures.EList [source]
Remove rows by which have the given indices.
- Parameters
indices (List[int]) –
- Returns
filtered_list
- Return type
- class mpu.datastructures.Interval(left: Optional[Any] = None, right: Optional[Any] = None)[source]
Bases:
mpu.datastructures.IntervalLike
Representation of an interval.
The empty interval is represented as left=None, right=None. Left and right have to be comparable. Typically, it would be numbers or dates.
- Parameters
left (Optional[Any]) –
right (Optional[Any]) –
- intersection(other: Interval) Interval [source]
- intersection(other: IntervalUnion) mpu.datastructures.IntervalLike
Intersect two IntervalLike objects.
- Parameters
other (IntervalLike) –
- Returns
intersected
- Return type
- issubset(other: mpu.datastructures.IntervalLike) bool [source]
Check if the interval “self” is completely inside of other.
- Parameters
other (IntervalLike) –
- Returns
is_inside
- Return type
bool
- union(other: mpu.datastructures.IntervalLike) mpu.datastructures.IntervalLike [source]
Combine two Intervals.
- Parameters
other (IntervalLike) –
- Returns
interval_union
- Return type
- class mpu.datastructures.IntervalLike[source]
Bases:
object
Anything like an interval or a union of an interval.
As mpu supports Python 2.7 until 2020 and does not want to include extra dependencies, ABC cannot be used.
- intersection(other: mpu.datastructures.IntervalLike) mpu.datastructures.IntervalLike [source]
Intersect two IntervalLike objects.
- Parameters
other (IntervalLike) –
- Returns
intersected
- Return type
- issubset(other: mpu.datastructures.IntervalLike) bool [source]
Check if the interval “self” is completely inside of other.
- Parameters
other (IntervalLike) –
- Returns
is_inside
- Return type
bool
- union(other: mpu.datastructures.IntervalLike) mpu.datastructures.IntervalLike [source]
Combine two Intervals.
- Parameters
other (IntervalLike) –
- Returns
interval_union
- Return type
- class mpu.datastructures.IntervalUnion(intervals)[source]
Bases:
mpu.datastructures.IntervalLike
A union of Intervals.
- intersection(other: mpu.datastructures.IntervalLike) mpu.datastructures.IntervalLike [source]
Return the intersection between this IntervalUnion and another object.
This changes the object itself!
- Parameters
other (Interval or IntervalUnion) –
- Returns
intersection
- Return type
- issubset(other: mpu.datastructures.IntervalLike) bool [source]
Check if this IntervalUnion is completely inside of other.
- Parameters
other (Interval or IntervalUnion) –
- Returns
is_inside
- Return type
bool
- union(other: mpu.datastructures.IntervalLike) mpu.datastructures.IntervalLike [source]
Return the union between this IntervalUnion and another object.
- Parameters
other (Interval or IntervalUnion) –
- Returns
union
- Return type
- mpu.datastructures.dict_merge(dict_left: Dict, dict_right: Dict, merge_method: str = 'take_left_shallow') Dict [source]
Merge two dictionaries.
This method does NOT modify dict_left or dict_right!
Apply this method multiple times if the dictionary is nested.
- Parameters
dict_left (Dict) –
dict_right (Dict) –
merge_method ({'take_left_shallow', 'take_left_deep', ) –
- ‘take_right_shallow’, ‘take_right_deep’,
’sum’}
take_left_shallow: Use both dictinaries. If both have the same key, take the value of dict_left
take_left_deep : If both dictionaries have the same key and the value is a dict for both again, then merge those sub-dictionaries
take_right_shallow : See take_left_shallow
take_right_deep : See take_left_deep
sum : sum up both dictionaries. If one does not have a value for a key of the other, assume the missing value to be zero.
- Returns
merged_dict
- Return type
Dict
Examples
>>> dict_merge({'a': 1, 'b': 2}, {'c': 3}) == {'a': 1, 'b': 2, 'c': 3} True
>>> out = dict_merge({'a': {'A': 1}}, ... {'a': {'A': 2, 'B': 3}}, 'take_left_deep') >>> expected = {'a': {'A': 1, 'B': 3}} >>> out == expected True
>>> out = dict_merge({'a': {'A': 1}}, ... {'a': {'A': 2, 'B': 3}}, 'take_left_shallow') >>> expected = {'a': {'A': 1}} >>> out == expected True
>>> out = dict_merge({'a': 1, 'b': {'c': 2}}, ... {'b': {'c': 3, 'd': 4}}, ... 'sum') >>> expected = {'a': 1, 'b': {'c': 5, 'd': 4}} >>> out == expected True
- mpu.datastructures.does_keychain_exist(dict_: Dict, list_: List) bool [source]
Check if a sequence of keys exist in a nested dictionary.
- Parameters
dict (Dict[str/int/tuple, Any]) –
list (List[str/int/tuple]) –
- Returns
keychain_exists
- Return type
bool
Examples
>>> d = {'a': {'b': {'c': 'd'}}} >>> l_exists = ['a', 'b'] >>> does_keychain_exist(d, l_exists) True
>>> l_no_existent = ['a', 'c'] >>> does_keychain_exist(d, l_no_existent) False
- mpu.datastructures.flatten(iterable: Iterable, string_flattening: bool = False) List [source]
Flatten an given iterable of iterables into one list.
- Parameters
iterable (Iterable) –
string_flattening (bool) – If this is False, then strings are NOT flattened
- Returns
flat_list
- Return type
List
Examples
>>> flatten([1, [2, [3]]]) [1, 2, 3]
>>> flatten(((1, 2), (3, 4), (5, 6))) [1, 2, 3, 4, 5, 6]
>>> flatten(EList([EList([1, 2]), (3, [4, [[5]]])])) [1, 2, 3, 4, 5]
- mpu.datastructures.set_dict_value(dictionary: Dict, keys: List[Any], value: Any) Dict [source]
Set a value in a (nested) dictionary by defining a list of keys.
Note
Side-effects This function does not make a copy of dictionary, but directly edits it.
- Parameters
dictionary (Dict) –
keys (List[Any]) –
value (Any) –
- Returns
dictionary
- Return type
dict
Examples
>>> d = {'a': {'b': {'c': 'x', 'f': 'g'}, 'd': 'e'}} >>> expected = {'a': {'b': {'c': 'foobar', 'f': 'g'}, 'd': 'e'}} >>> set_dict_value(d, ['a', 'b', 'c'], 'foobar') == expected True