Dict operators¶
commons.dict_operators
¶
Functions:
| Name | Description |
|---|---|
deep_merge |
Recursively merge two dictionaries, with override values taking precedence. |
unflatten |
Convert a flat dictionary with dotted keys to a nested structure. |
flatten |
Convert a nested dictionary to a flat dictionary with dotted keys. |
deep_merge
¶
Recursively merge two dictionaries, with override values taking precedence.
When both base and override have a dict value for the same key, the dicts are merged recursively. For all other cases (non-dict values, or key only in one dict), the override value replaces the base value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
dict
|
The base dictionary to merge into. |
required |
override
|
dict
|
The dictionary whose values take precedence. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A new dictionary with merged values. Neither input dict is mutated. |
Source code in src/commons/dict_operators.py
unflatten
¶
Convert a flat dictionary with dotted keys to a nested structure.
Supports: - Dot notation: "foo.bar" -> {"foo": {"bar": ...}} - List indices: "items[0]" -> {"items": [...]} - Bracket keys: 'data["key"]' -> {"data": {"key": ...}}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A flat dictionary with dotted/bracketed keys. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A nested dictionary structure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If list indices are not contiguous (e.g., index 1 exists but index 0 is missing). |
Source code in src/commons/dict_operators.py
flatten
¶
Convert a nested dictionary to a flat dictionary with dotted keys.
Inverse of unflatten().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A nested dictionary structure. |
required |
prefix
|
str
|
Internal prefix for recursive calls. |
''
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A flat dictionary with dotted/bracketed keys. |