def get_recursive_properties(cur, node): """ Get the entire inherited and recursively merged property dictionary. To calculate this, the trees path from root node till ``node`` will be traversed. For each level, the property dictionary will be merged into the previous one. This is a recursive merge, so all dictionary levels will be combined. :param node: :type node: Node or uuid4 :rtype: dict """ ret = {} id = str(node) if isinstance(node, str): node = get_node(cur, id) ancestors = list(get_ancestors(cur, id)) for ancestor in ancestors[::-1]: # Go top down recursive_dict_merge(ret, ancestor.properties, create_copy=False) recursive_dict_merge(ret, node.properties, create_copy=False) return ret
def test_recursive_dict_merge(): left = { 1: {'a': 'A'}, 2: {'b': 'B'}, 3: {'c': {'c': 'C'}} } right = { 2: {'b': 'b', 'c': 'C'}, 3: {'c': {'c': 'c'}, 'd': 'D'} } expected = { 1: {'a': 'A'}, 2: {'b': 'b', 'c': 'C'}, 3: {'c': {'c': 'c'}, 'd': 'D'} } assert recursive_dict_merge(left, right) == expected
def test_recursive_dict_merge(): left = {1: {'a': 'A'}, 2: {'b': 'B'}, 3: {'c': {'c': 'C'}}} right = {2: {'b': 'b', 'c': 'C'}, 3: {'c': {'c': 'c'}, 'd': 'D'}} expected = { 1: { 'a': 'A' }, 2: { 'b': 'b', 'c': 'C' }, 3: { 'c': { 'c': 'c' }, 'd': 'D' } } assert recursive_dict_merge(left, right) == expected