mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Add helpers to merge dictionaries and get keys deep inside a dictionary
This commit is contained in:
parent
eddbc67358
commit
bafc86d583
2 changed files with 49 additions and 0 deletions
30
src/tests/test_helpers.py
Normal file
30
src/tests/test_helpers.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
from utils import helpers
|
||||||
|
|
||||||
|
def test_get_from_null_dict():
|
||||||
|
# null handling
|
||||||
|
assert helpers.get_from_dict(dict()) == dict()
|
||||||
|
assert helpers.get_from_dict(dict(), None) == None
|
||||||
|
|
||||||
|
# key present in nested dictionary
|
||||||
|
# 1-level dictionary
|
||||||
|
assert helpers.get_from_dict({'a': 1, 'b': 2}, 'a') == 1
|
||||||
|
assert helpers.get_from_dict({'a': 1, 'b': 2}, 'c') == None
|
||||||
|
|
||||||
|
# 2-level dictionary
|
||||||
|
assert helpers.get_from_dict({'a': {'a_a': 1}, 'b': 2}, 'a') == {'a_a': 1}
|
||||||
|
assert helpers.get_from_dict({'a': {'a_a': 1}, 'b': 2}, 'a', 'a_a') == 1
|
||||||
|
|
||||||
|
# key not present in nested dictionary
|
||||||
|
# 2-level_dictionary
|
||||||
|
assert helpers.get_from_dict({'a': {'a_a': 1}, 'b': 2}, 'b', 'b_a') == None
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_dicts():
|
||||||
|
# basic merge of dicts with non-overlapping keys
|
||||||
|
assert helpers.merge_dicts(priority_dict={'a': 1}, default_dict={'b': 2}) == {'a': 1, 'b': 2}
|
||||||
|
|
||||||
|
# use default dict items when not present in priority dict
|
||||||
|
assert helpers.merge_dicts(priority_dict={}, default_dict={'b': 2}) == {'b': 2}
|
||||||
|
|
||||||
|
# do not override existing key in priority_dict with default dict
|
||||||
|
assert helpers.merge_dicts(priority_dict={'a': 1}, default_dict={'a': 2}) == {'a': 1}
|
|
@ -7,3 +7,22 @@ def is_none_or_empty(item):
|
||||||
|
|
||||||
def get_absolute_path(filepath):
|
def get_absolute_path(filepath):
|
||||||
return str(pathlib.Path(filepath).expanduser().absolute())
|
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
|
||||||
|
|
Loading…
Reference in a new issue