mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-05 21:29:11 +00:00
Add helpers to merge dictionaries and get keys deep inside a dictionary
This commit is contained in:
@@ -7,3 +7,22 @@ def is_none_or_empty(item):
|
||||
|
||||
def get_absolute_path(filepath):
|
||||
return str(pathlib.Path(filepath).expanduser().absolute())
|
||||
|
||||
|
||||
def get_from_dict(dictionary, *args):
|
||||
'''null-aware get from a nested dictionary
|
||||
Returns: dictionary[args[0]][args[1]]... or None if any keys missing'''
|
||||
current = dictionary
|
||||
for arg in args:
|
||||
if not hasattr(current, '__iter__') or not arg in current:
|
||||
return None
|
||||
current = current[arg]
|
||||
return current
|
||||
|
||||
|
||||
def merge_dicts(priority_dict, default_dict):
|
||||
merged_dict = priority_dict.copy()
|
||||
for k, v in default_dict.items():
|
||||
if k not in priority_dict:
|
||||
merged_dict[k] = default_dict[k]
|
||||
return merged_dict
|
||||
|
||||
Reference in New Issue
Block a user