def _merge_mapping(merge_to, merge_from, inplace=False):
"""
As opposed to dict.update, this will recurse through a dictionary's values merging new keys with old ones
This is an in-place merge.
:arg merge_to: dictionary to merge into
:arg merge_from: dictionary to merge from
:kwarg inplace: If True, merge_to will be modified directly. If False, a copy of merge_to will
be modified.
:returns: the combined dictionary. If inplace is True, then this is the same as merge_to after
calling this function
"""
if inplace:
dest = merge_to
else:
dest = merge_to.copy()
for key, val in list(merge_from.items()):
if key in dest and isinstance(dest[key], MutableMapping) and \
isinstance(val, MutableMapping):
# Dict value so merge the value
dest[key] = _merge_mapping(dest[key], val, inplace=inplace)
else:
dest[key] = val
return dest
评论列表
文章目录