def merge_dict(target, *sources):
"""
Merge the given list of `Mapping` objects into `target` object.
:param MutableMapping target: The mapping to receive the merge.
:param tuple sources: The list of `mapping` objects to be merged.
"""
for source in sources:
if not isinstance(target, MutableMapping):
raise TypeError('target must be a dict')
if not isinstance(source, Mapping):
raise TypeError('data must be a dict')
for key in source:
target_value = target.get(key)
source_value = source[key]
if target_value is None or source_value is None:
target[key] = source_value
elif isinstance(target_value, Mapping) and isinstance(source_value, Mapping):
merge_dict(target_value, source_value)
else:
target[key] = source_value
评论列表
文章目录