Skip to content
Snippets Groups Projects
Commit fa237995 authored by Malte Nyhuis's avatar Malte Nyhuis
Browse files

additional dict_utils copied from ntr

parent 8eb87298
No related branches found
No related tags found
No related merge requests found
from functools import reduce from functools import reduce
import operator import operator
def delete_keys_from_dict(dictionary, key_list):
"""
delets set of keys from dictionary
"""
dictionary = reduce(dict.get, key_list[0:-1], dictionary)
del dictionary[key_list[-1]]
def nested_dict_pairs_iterator(dict_obj): def nested_dict_pairs_iterator(dict_obj):
''' This function accepts a nested dictionary as argument '''
and iterate over all values of nested dictionaries This function accepts a nested dictionary as argument
and iterate over all values of nested dictionaries
''' '''
# Iterate over all key-value pairs of dict argument # Iterate over all key-value pairs of dict argument
for key, value in dict_obj.items(): for key, value in dict_obj.items():
...@@ -28,3 +35,66 @@ def setInDict(dataDict, mapList, value): ...@@ -28,3 +35,66 @@ def setInDict(dataDict, mapList, value):
def getFromDict(dataDict, mapList): def getFromDict(dataDict, mapList):
#todo: test-method #todo: test-method
return reduce(operator.getitem, mapList, dataDict) return reduce(operator.getitem, mapList, dataDict)
def merge(a, b, path=None):
"merges b into a"
if path is None: path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
else:
a[key] = b[key]
return a
def appendInDictList(dataDict, mapList, value):
"""
appends value to nested dict with list-values
"""
getFromDict(dataDict, mapList[:-1])[mapList[-1]].append(value)
def nested_val_set(dic, keys, value):
for key in keys[:-1]:
dic = dic.setdefault(key, {})
dic[keys[-1]] = value
def compare_dictionaries(dict_1, dict_2,path=""):
"""Compare two dictionaries recursively to find non mathcing elements
https://stackoverflow.com/questions/27265939/comparing-python-dictionaries-and-nested-dictionaries
Args:
dict_1: dictionary 1
dict_2: dictionary 2
Returns:
"""
key_err = 0
value_err = 0
old_path = path
for k in dict_1.keys():
path = old_path + "[%s]" % k
if not k in dict_2.keys():
key_err += 1
else:
if isinstance(dict_1[k], dict) and isinstance(dict_2[k], dict):
ke,ve =compare_dictionaries(dict_1[k],dict_2[k], path)
key_err+=ke
value_err +=ve
else:
if dict_1[k] != dict_2[k]:
value_err += 1
for k in dict_2.keys():
path = old_path + "[%s]" % k
if not k in dict_1.keys():
key_err += 1
return key_err ,value_err
...@@ -7,6 +7,7 @@ import pyvista as pv ...@@ -7,6 +7,7 @@ import pyvista as pv
import numpy as np import numpy as np
from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator
from utils.dictionaries.dict_utils import delete_keys_from_dict, compare_dictionaries
@pytest.fixture @pytest.fixture
...@@ -148,3 +149,19 @@ def test_nested_dict_pairs_iterator(): ...@@ -148,3 +149,19 @@ def test_nested_dict_pairs_iterator():
testdict = {"0":{"0":0,"1":1},"1":1} testdict = {"0":{"0":0,"1":1},"1":1}
check = [('0', '0', 0), ('0', '1', 1), ('1', 1)] check = [('0', '0', 0), ('0', '1', 1), ('1', 1)]
assert check == list(nested_dict_pairs_iterator(testdict)), "error" assert check == list(nested_dict_pairs_iterator(testdict)), "error"
def test_delete_keys_from_dict():
dict = {"a":0,"b":1}
delete_keys_from_dict(dict, "a")
assert "a" not in dict.keys()
assert "b" in dict.keys()
assert dict["b"]==1
def test_compare_dictionaries():
d1 = {"name":{"param_1":0,"param_2":3}}
d2 = {"name":{"param_1":4,"param_2":1}}
ke,ve = compare_dictionaries(d1,d2)
assert ke == 0
assert ve == 2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment