deep_merge implemented

recursively merges dicts in profiles

patch bump
This commit is contained in:
2023-07-12 04:52:42 +01:00
parent b0acde6a52
commit 278566c2e0
3 changed files with 25 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
import copy
import functools
from itertools import zip_longest
from typing import Iterator
@@ -70,3 +71,17 @@ def grouper(n, iterable, fillvalue=None):
"""
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
def deep_merge(dict1, dict2):
"""Generator function for deep merging two dicts"""
for k in set(dict1) | set(dict2):
if k in dict1 and k in dict2:
if isinstance(dict1[k], dict) and isinstance(dict2[k], dict):
yield k, dict(deep_merge(dict1[k], dict2[k]))
else:
yield k, dict2[k]
elif k in dict1:
yield k, dict1[k]
else:
yield k, dict2[k]