Created
July 17, 2019 01:47
-
-
Save oiotoxt/04a39919b0efeedc8e6edbdced43f004 to your computer and use it in GitHub Desktop.
Recursive dict merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import absolute_import | |
| if __name__ != "__main__": print(__name__, "imported.") | |
| else: print(__file__, "is being executed directly.") | |
| import collections | |
| from copy import deepcopy | |
| import json5 as json # pip install json5 ( >= v0.8.0 ) | |
| def merge_dict(dict1, dict2, add_keys=True): | |
| """ Recursive dict merge. | |
| Ref: | |
| https://gist.github.com/angstwad/bf22d1822c38a92ec0a9#gistcomment-2622319 | |
| """ | |
| merged = deepcopy(dict1) | |
| if not add_keys: | |
| common = { k: dict2[k] for k in set(dict1).intersection(set(dict2)) } | |
| for k, v in common.items(): | |
| if isinstance(merged.get(k), dict) and isinstance(v, collections.Mapping): | |
| merged[k] = merge_dict(merged[k], v, add_keys=add_keys) | |
| else: | |
| merged[k] = v | |
| return merged | |
| def merge_dicts(dict_list, add_keys=True): | |
| assert isinstance(dict_list, list) | |
| merged = dict_list[0] | |
| for idx in range(len(dict_list) - 1): | |
| merged = merge_dict(merged, dict_list[idx+1], add_keys=add_keys) | |
| return merged | |
| def merge_dicts_in_json(json_path, key_list, add_keys=True): | |
| assert isinstance(key_list, list) | |
| try: | |
| json_data = json.load(open(json_path, encoding='utf-8'), allow_duplicate_keys=False) | |
| # print(f"json_data == {json.dumps(json_data, indent=4)}") | |
| dict_list = [json_data[key] for key in key_list] | |
| return merge_dicts(dict_list, add_keys=add_keys) | |
| except Exception as e: | |
| raise Exception("Failed to read the configuration file. Exception={{ {0} }}".format(str(e))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment