diff --git a/ntrfc/utils/dictionaries/dict_utils.py b/ntrfc/utils/dictionaries/dict_utils.py index 5224d62381b5a0cd7b8bdab9c9d954de6dc51487..2907455f97d1cf87982faa1c309b13cda9ff767d 100644 --- a/ntrfc/utils/dictionaries/dict_utils.py +++ b/ntrfc/utils/dictionaries/dict_utils.py @@ -1,11 +1,18 @@ from functools import reduce 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): - ''' 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 for key, value in dict_obj.items(): @@ -28,3 +35,66 @@ def setInDict(dataDict, mapList, value): def getFromDict(dataDict, mapList): #todo: test-method 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 + diff --git a/tests/test_ntrfc.py b/tests/test_ntrfc.py index 9a162d53eeeb6e8e9c6e8733df11b08c70881967..a093c08e23fa101978d00f6273f2959c311c358f 100644 --- a/tests/test_ntrfc.py +++ b/tests/test_ntrfc.py @@ -7,6 +7,7 @@ import pyvista as pv import numpy as np 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 @@ -148,3 +149,19 @@ def test_nested_dict_pairs_iterator(): testdict = {"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" + + +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