impl#

In configuration management, it is common practice to divide the configuration into multiple files. From a machine’s perspective, merging multiple dictionary data into one can be more convenient to process it later. This module is designed to recursively merge two dictionaries or a list of dictionaries.

config_patterns.patterns.merge_key_value.impl.merge_key_value(data1: dict, data2: dict, _fullpath: Optional[str] = None) dict[source]#

Merge two dict recursively. Both dict are equally important. Note that the original data will NOT be modified, it copy the data and return a new dict (the merged one).

Parameters:
  • data1 – dict data 1.

  • data2 – dict data 2.

Example:

>>> data1 = {
...     "key1": "value1",
...     "credentials": [
...         {"username": "alice"},
...         {"username": "bob"},
...     ]
... }
>>> data2 = {
...     "key2": "value2",
...     "credentials": [
...         {"password": "alice.pwd"},
...         {"password": "bob.pwd"},
...     ]
... }
>>> merge_key_value(data1, data2)
{
    "key1": "value1",
    "key2": "value2",
    "credentials": [
        {
            "username": "alice",
            "password": "alice.pwd",
        },
        {
            "username": "bob",
            "password": "bob.pwd",
        },
    ],
}