mpu.datastructures

Utility datastructures.

class mpu.datastructures.EList(*args)[source]

Bases: list

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)[source]

Remove rows by which have the given indices.

Parameters

indices (list) –

Returns

filtered_list

Return type

EList

class mpu.datastructures.Interval(left=None, right=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 (object) –

  • right (object) –

intersection(other)[source]

Intersect two IntervalLike objects.

Parameters

other (IntervalLike) –

Returns

intersected

Return type

IntervalLike

is_empty()[source]

Return if the interval is empty.

issubset(other)[source]

Check if the interval “self” is completely inside of other.

Parameters

other (IntervalLike) –

Returns

is_inside

Return type

bool

union(other)[source]

Combine two Intervals.

Parameters

other (IntervalLike) –

Returns

interval_union

Return type

IntervalLike

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)[source]

Intersect two IntervalLike objects.

Parameters

other (IntervalLike) –

Returns

intersected

Return type

IntervalLike

is_empty()[source]

Return if the IntervalLike is empty.

issubset(other)[source]

Check if the interval “self” is completely inside of other.

Parameters

other (IntervalLike) –

Returns

is_inside

Return type

bool

union(other)[source]

Combine two Intervals.

Parameters

other (IntervalLike) –

Returns

interval_union

Return type

IntervalLike

class mpu.datastructures.IntervalUnion(intervals)[source]

Bases: mpu.datastructures.IntervalLike

A union of Intervals.

intersection(other)[source]

Return the intersection between this IntervalUnion and another object.

This changes the object itself!

Parameters

other (Interval or IntervalUnion) –

Returns

intersection

Return type

Interval or IntervalUnion

is_empty()[source]

Return if the IntervalUnion is empty.

issubset(other)[source]

Check if this IntervalUnion is completely inside of other.

Parameters

other (Interval or IntervalUnion) –

Returns

is_inside

Return type

bool

union(other)[source]

Return the union between this IntervalUnion and another object.

Parameters

other (Interval or IntervalUnion) –

Returns

union

Return type

Interval or IntervalUnion

mpu.datastructures.dict_merge(dict_left, dict_right, merge_method='take_left_shallow')[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_, list_)[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_existant = ['a', 'c']
>>> does_keychain_exist(d, l_no_existant)
False
mpu.datastructures.flatten(iterable, string_flattening=False)[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, keys, value)[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 (object) –

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