2022-03-24 20:45:29 +00:00
|
|
|
"""Utility functions for working with Dict-type structures."""
|
|
|
|
|
from typing import MutableMapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_dicts(dict1, dict2):
|
|
|
|
|
"""Recursively merges dict2 into dict1."""
|
|
|
|
|
if not (isinstance(dict1, MutableMapping) and isinstance(dict2, MutableMapping)):
|
|
|
|
|
return dict2
|
|
|
|
|
|
|
|
|
|
for k in dict2.keys():
|
|
|
|
|
if dict2[k] is None:
|
2023-02-21 13:20:29 +00:00
|
|
|
if k in dict1:
|
|
|
|
|
dict1.pop(k)
|
2022-03-24 20:45:29 +00:00
|
|
|
elif k in dict1:
|
|
|
|
|
dict1[k] = merge_dicts(dict1[k], dict2[k])
|
|
|
|
|
else:
|
|
|
|
|
dict1[k] = dict2[k]
|
|
|
|
|
return dict1
|
2023-01-19 00:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_dict_value(dict1, path):
|
|
|
|
|
current_object = dict1
|
|
|
|
|
|
|
|
|
|
for key in path:
|
|
|
|
|
if key not in current_object:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
current_object = current_object[key]
|
|
|
|
|
|
|
|
|
|
return current_object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_dict_value(dict1, path, value):
|
|
|
|
|
current_object = dict1
|
|
|
|
|
|
|
|
|
|
for key in path[:-1]:
|
|
|
|
|
if key not in path:
|
|
|
|
|
current_object[key] = {}
|
|
|
|
|
|
|
|
|
|
current_object = current_object[key]
|
|
|
|
|
|
|
|
|
|
current_object[path[-1]] = value
|