collections
Custom data structures and related functions.
Classes
FlattenKeyDictView
FlattenKeyDictView(
*args, flat_key_separator: str = ".", **kwargs
)
Bases: dict
Dictionary view which works with flatten keys.
This class allows accessing values of a normal Python nested dictionary
using flatten keys, e.g. a.b.c, parent.child. The dictionary view
implements getting, setting, deleting and checking existance of keys.
Example usage:
from ipsl_common.collections import FlattenKeyDictView
d = {
"a": {
"k1": 1,
"k2": 2
}
}
fd = FlattenKeyDictView(d)
fd["a.k1"] = 17
del fd["a.k2"]
fd["b.k1"] = 34
print(fd)
Which prints the following result:
{'a': {'k1': 17}, 'b': {'k1': 34}}
Create flatten-key dict view class.
Parameters:
-
–*argspositional arguments passed to the dict() constructor
-
(flat_key_separatorstr, default:'.') –separator of keys in the flat key (default:
.) -
–**kwargskeyword arguments passed to the dict() constructor
Source code in ipsl_common/collections.py
83 84 85 86 87 88 89 90 91 92 | |
Attributes
flat_key_separator
instance-attribute
flat_key_separator = flat_key_separator
Methods:
__contains__
__contains__(flat_key: object) -> bool
Check if flat key exists
Source code in ipsl_common/collections.py
117 118 119 120 121 122 123 124 125 | |
__delitem__
__delitem__(flat_key: str) -> None
Delete key-value pairs using flat key
Source code in ipsl_common/collections.py
112 113 114 115 | |
__getitem__
__getitem__(flat_key: str) -> Any
Retrieve value using flat key
Source code in ipsl_common/collections.py
102 103 104 105 | |
__setitem__
__setitem__(flat_key: str, value: Any) -> None
Set new value using flat key
Source code in ipsl_common/collections.py
107 108 109 110 | |
ordered_keys
ordered_keys() -> list[str]
Ordered list of all flatten keys (including all parent keys)
Source code in ipsl_common/collections.py
94 95 96 | |
ordered_leaf_keys
ordered_leaf_keys() -> list[str]
Ordered list of flatten leaf keys (excluding all parent keys)
Source code in ipsl_common/collections.py
98 99 100 | |
Functions:
deep_update
deep_update(target: dict, update: dict) -> dict
Deep update of a dictionary.
Update the target dictionary in a nested manner. The standard dict.update() method performs only a shallow update, meaning that only the top-level dictionary keys are updated. This function will recursively inspect the dict values, so that the nested values are correctly updated.
This function doesn't perform any deep copy before updating the target dictionary.
Hence, all the updates on the target variable are performed in-place.
Parameters:
-
(targetdict) –the dictionary to update
-
(updatedict) –modifications to apply
Returns:
-
dict(dict) –returned updated dictionary (the same as the
targetargument reference)
Source code in ipsl_common/collections.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
select_values_by_keys
select_values_by_keys(
mapping: dict, *args: list[Any] | tuple[Any]
) -> list[Any]
Select mapping values from a collection of keys.
Mapping is usally a dictionary, while the collection of keys can be, either, passed as many *args, or a single list or tuple.
Parameters:
-
(mappingdict) –dictionary to select values from
-
(*argslist[Any] | tuple[Any], default:()) –keys used for the selection
Returns:
-
list[Any]–list[Any]: collection of values selected by their keys
Source code in ipsl_common/collections.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |