flib.base.mapping module

Helper functions for mappings.

crop(d: Mapping[KT, VT_co], prefix: str, trim: bool = True) → dict

Crop mapping to keys, that start with an initial string.

  • d – Mapping that encodes sections by the prefix of string keys
  • prefix – Key prefix as string
  • trim – Determines if the section prefix is removed from the keys of the returned mapping. Default: True

Subset of the original mapping, which only contains keys that start with the given section. Thereby the new keys are trimmed from the initial section string.


>>> crop({'a1': 1, 'a2': 2, 'b1': 3}, 'a')
{'1': 1, '2': 2}
flatten(d: Dict[Any, Dict[Any, Dict[str, Any]]], group: Optional[str] = None) → Dict[Any, Dict[str, Any]]

Flatten grouped record dictionary by given group name.

Inverse dictionary operation to ‘groupby’.

  • d – Nested dictionary, which entries are interpreted as attributes.
  • group – Attribute names, which describes the groups.

Dictinary which flattens the groups of the original dictinary to attributes.


>>> flatten({1: {'a': {}}, 2: {'b': {}}})
{'a': {}, 'b': {}}
>>> flatten({1: {'a': {}}, 2: {'b': {}}}, group='id')
{'a': {'id': 1}, 'b': {'id': 2}}
groupby(d: Dict[Any, Dict[str, Any]], key: str, rmkey: bool = False) → Dict[Any, Dict[Any, Dict[str, Any]]]

Group record dictionary by the value of a given key.

  • d – Dictionary of dictionaries, which entries are interpreted as attributes.
  • key – Name of attribute which is used to group the results by it’s corresponding value.
  • rmkey – Boolean which determines, if the group attribute is removed from the the sub dictionaries.

Dictinary which groups the entries of the original dictinary in subdictionaries.

merge(*args, mode: int = 1) → Mapping[KT, VT_co]

Recursively right merge mappings.

  • *args – Mappings with arbitrary structure
  • mode – Creation mode for merged mapping: 0: change rightmost dictionary 1: create new dictionary by deepcopy 2: create new dictionary by chain mapping

Dictionary containing right merge of dictionaries.


>>> merge({'a': 1}, {'a': 2, 'b': 2}, {'c': 3})
{'a': 1, 'b': 2, 'c': 3}
select(d: Mapping[KT, VT_co], pattern: str) → dict

Filter mappings to keys, that match a given pattern.

  • d – Mapping, which keys aregiven by strings
  • pattern – Wildcard pattern as described in the standard library module fnmatch.

Subset of the original mapping, which only contains keys, that match the given pattern.


>>> select({'a1': 1, 'a2': 2, 'b1': 3}, 'a*')
{'a1': 1, 'a2': 2}
strkeys(d: dict) → Dict[Union[str, Tuple[str, ...]], Any]

Recursively convert dictionary keys to string keys.

Parameters:d – Hierarchivally structured dictionary with keys of arbitrary types.
Returns:New dictionary with string converted keys. Thereby keys of type tuple are are not converted as a whole but with respect to the tokens in the tuple.


>>> strkeys({(1, 2): 3, None: {True: False}})
{('1', '2'): 3, 'None': {'True': False}}
sumjoin(*args) → dict

Sum values of common keys in differnet dictionaries.

Parameters:*args – dictionaries, that are recursively right merged
Returns:New dictionary, where items with keys, that only occur in a single dictionary are adopted and items with keys, that occur in multiple dictionaries are united by a sum.


>>> sumjoin({'a': 1}, {'a': 2, 'b': 3})
{'a': 3, 'b': 3}
>>> sumjoin({1: 'a', 2: True}, {1: 'b', 2: True})
{1: 'ab', 2: 2}