Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/traitlets/utils/nested_update.py: 9%
11 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1# Copyright (c) IPython Development Team.
2# Distributed under the terms of the Modified BSD License.
5def nested_update(this, that):
6 """Merge two nested dictionaries.
8 Effectively a recursive ``dict.update``.
10 Examples
11 --------
12 Merge two flat dictionaries:
13 >>> nested_update(
14 ... {'a': 1, 'b': 2},
15 ... {'b': 3, 'c': 4}
16 ... )
17 {'a': 1, 'b': 3, 'c': 4}
19 Merge two nested dictionaries:
20 >>> nested_update(
21 ... {'x': {'a': 1, 'b': 2}, 'y': 5, 'z': 6},
22 ... {'x': {'b': 3, 'c': 4}, 'z': 7, '0': 8},
23 ... )
24 {'x': {'a': 1, 'b': 3, 'c': 4}, 'y': 5, 'z': 7, '0': 8}
26 """
27 for key, value in this.items():
28 if isinstance(value, dict):
29 if key in that and isinstance(that[key], dict):
30 nested_update(this[key], that[key])
31 elif key in that:
32 this[key] = that[key]
34 for key, value in that.items():
35 if key not in this:
36 this[key] = value
38 return this