Coverage for merge_dicts.py: 100%
16 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-25 20:25 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-25 20:25 +0000
1"""
2merge_dicts - Given dictionaries are merge into a new dict.
3"""
5__version__ = "0.2.0"
7def merge_dicts(*dict_args: [{}]) -> [{}, bool]:
8 """
9 merge_dicts: Given dictionaries are merge into a new dict.
11 :param dict_args: Optional "dict_args" as list of dict.
12 :type dict_args: dict or list(dict) or None
13 :raise AttributeError: If the dict_args is invalid.
14 :return: merged dict and list of merge conflicts.
15 :rtype: list(dict, list)
16 """
18 result = {}
19 merge_conflict = False
20 merge_conflict_lists = []
22 for dictionary in dict_args:
23 current_list_of_merge_conflicts = []
24 for key, value in dictionary.items():
25 # Evaluate if two input dict have the same key, but different value.
26 # If so, add key to current_list_of_merge_conflicts.
27 if key in result:
28 if value == result[key]:
29 pass
30 else:
31 current_list_of_merge_conflicts.append(key)
32 merge_conflict = True
33 else:
34 # Add key and value to the dict.
35 result[key] = value
36 merge_conflict_lists.append(current_list_of_merge_conflicts)
37 return [result, merge_conflict]