aiida_vasp.utils.dict_merge#
Functions to merge dictionaries
Module Contents#
Functions#
Recursively merge two dictionaries into a single dictionary. |
|
Recursively merge two dictionaries into a single dictionary, supporting special operations for keys. |
API#
- aiida_vasp.utils.dict_merge.recursive_merge_orig(left: dict, right: dict) dict[source]#
Recursively merge two dictionaries into a single dictionary.
If any key is present in both
leftandrightdictionaries, the value from therightdictionary is assigned to the key.- Parameters:
left – first dictionary
right – second dictionary
- Returns:
the recursively merged dictionary
- aiida_vasp.utils.dict_merge.recursive_merge(left: dict, right: dict) dict[source]#
Recursively merge two dictionaries into a single dictionary, supporting special operations for keys.
If a key is present in both
leftandrightdictionaries, the value fromrightis used. If both values are dictionaries, they are merged recursively.Special operations (when the value in
rightis a dictionary with one of these keys):$!del: Delete this key from the result.$!append: Append a value to a list at this key.$!extend: Extend a list at this key with another list.$!replace: Replace the value at this key entirely.
- Parameters:
- Returns:
The recursively merged dictionary.
- Return type:
Examples
Basic merge:
>>> recursive_merge({'a': 1, 'b': {'c': 2}}, {'b': {'c': 3}, 'd': 4}) {'a': 1, 'b': {'c': 3}, 'd': 4}
Nested merge and list extend:
>>> recursive_merge({'a': {'x': 1, 'y': {'z': 2}}, 'b': [1, 2]}, {'a': {'y': {'z': 3}}, 'b': {'$!extend': [3, 4]}}) {'a': {'x': 1, 'y': {'z': 3}}, 'b': [1, 2, 3, 4]}
Special: append and delete:
>>> recursive_merge({'a': [1, 2], 'b': {'c': 5}}, {'a': {'$!append': 3}, 'b': {'$!del': True}}) {'a': [1, 2, 3]} >>> recursive_merge({'a': [1, 2], 'b': {'c': 5}}, {'a': {'$!append': 3}, 'b': '$!del'}}) {'a': [1, 2, 3]}
Replace:
>>> recursive_merge({'a': {'x': 1}}, {'a': {'$!replace': {'y': 2}}}) {'a': {'y': 2}}