Dict Tools¶
Functions for manipulating dictionaries.
convert_key_case ¶
convert_key_case(data, key_case)
Recursively convert dictionary keys using a case-conversion function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
T
|
A dict, list, or scalar value. |
required |
key_case
|
Callable[[str], str]
|
A callable that transforms string keys (e.g., |
required |
Returns:
| Type | Description |
|---|---|
T
|
A copy of |
Source code in src/aibs_informatics_core/utils/tools/dicttools.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
flatten_dict ¶
flatten_dict(data, parent_key='', delimiter='.')
Flattens a nested dictionary by concatenating the keys with a given delimiter.
Args: data (Dict[str, Any]): The dictionary to flatten. parent_key (str, optional): The concatenated key for the nested items. Defaults to ''. delimiter (str, optional): The delimiter to use for concatenating the keys. Defaults to '.'.
Returns: Dict[str, Any]: The flattened dictionary.
Source code in src/aibs_informatics_core/utils/tools/dicttools.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
nested_dict ¶
nested_dict(data, delimiter='.')
Creates a nested dictionary by splitting the keys by a given delimiter.
Args: data (Dict[str, Any]): The dictionary to transform. delimiter (str, optional): The delimiter to use for splitting the keys. Defaults to '.'.
Returns: Dict[str, Any]: The transformed, nested dictionary.
Raises: ValueError: If a collision is detected during the transformation process.
Source code in src/aibs_informatics_core/utils/tools/dicttools.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
remove_matching_values ¶
remove_matching_values(
orig_dict,
in_place=False,
recursive=False,
target_value=None,
)
Filter out keys with values matching target value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orig_dict
|
MutableMapping[KT, VT]
|
Original dictionary |
required |
in_place
|
bool
|
If true, removes keys in place. Defaults to False. |
False
|
recursive
|
bool
|
If true, recursively removes values if target value. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
MutableMapping[KT, VT]
|
MutableMapping[KT, VT]: dictionary without target values |
Source code in src/aibs_informatics_core/utils/tools/dicttools.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
remove_null_values ¶
remove_null_values(
orig_dict, in_place=False, recursive=False
)
Removes null values from a dictionary object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orig_dict
|
MutableMapping[KT, VT]
|
target dictionary to prune null values from |
required |
in_place
|
bool
|
whether to do operation on target in place. Defaults to False. |
False
|
recursive
|
bool
|
whether to prune recursively. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
MutableMapping[KT, VT]
|
MutableMapping[KT, VT]: pruned dictionary |
Source code in src/aibs_informatics_core/utils/tools/dicttools.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |