def test_get_attribute_schema_with_nested_dictionaries():
    input_list = [('0', {
        'attributes': {
            'osm:way:highway': {
                'name': 'osm:way:highway',
                'class': 'java.lang.String',
                'text': 'primary'
            }
        }
    }),
                  ('1', {
                      'attributes': {
                          'osm:way:id': {
                              'name': 'osm:way:id',
                              'class': 'java.lang.String',
                              'text': '1234'
                          }
                      }
                  })]

    root = graph_operations.get_attribute_schema(input_list)

    output_tree = generate_output_tree(root)

    assert output_tree == [(0, None, 'attribute'),
                           (1, 'attribute', 'attributes'),
                           (2, 'attributes', 'osm:way:highway'),
                           (3, 'osm:way:highway', 'name'),
                           (3, 'osm:way:highway', 'class'),
                           (3, 'osm:way:highway', 'text'),
                           (2, 'attributes', 'osm:way:id'),
                           (3, 'osm:way:id', 'name'),
                           (3, 'osm:way:id', 'class'),
                           (3, 'osm:way:id', 'text')]
def test_get_attribute_schema_with_different_in_a_nested_dictionary_with_same_keys(
):
    input_list = [('0', {
        'attributes': {
            'osm:way:highway': {
                'name': 'osm:way:highway',
                'class': 'java.lang.String',
                'text': 'primary'
            }
        }
    }),
                  ('1', {
                      'attributes': {
                          'osm:way:highway': {
                              'name': 'osm:way:highway',
                              'class': 'java.lang.String',
                              'text': 'secondary'
                          }
                      }
                  })]

    root = graph_operations.get_attribute_schema(input_list, data=True)

    output_tree = generate_output_tree(root)

    assert output_tree == [
        (0, None, 'attribute'), (1, 'attribute', 'attributes'),
        (2, 'attributes', 'osm:way:highway'),
        (3, 'osm:way:highway', 'name', {'osm:way:highway'}),
        (3, 'osm:way:highway', 'class', {'java.lang.String'}),
        (3, 'osm:way:highway', 'text', {'primary', 'secondary'})
    ]
Example #3
0
def find_nested_paths_to_value(d: dict, value: Union[str, int, float, set, list]):
    def parse_node_path(node_path):
        n_path_names_reversed = list(reversed([n.name for n in node_path if n.name != 'attribute']))
        if len(n_path_names_reversed) == 1:
            return n_path_names_reversed[0]
        else:
            d = {n_path_names_reversed[1]: n_path_names_reversed[0]}
            for key in n_path_names_reversed[2:]:
                d = {key: d}
            return d

    if not isinstance(value, (list, set)):
        value = {value}
    elif not isinstance(value, set):
        value = set(value)
    paths = []
    schema = graph_operations.get_attribute_schema([('', d)], data=True)
    for node in schema.descendants:
        try:
            if node.values & value:
                paths.append(parse_node_path(node.path))
        except AttributeError:
            # that node does not have values
            pass
    return paths
def test_get_attribute_schema_merges_lists():
    input_list = [('0', {
        'modes': ['car']
    }), ('1', {
        'modes': ['walk', 'bike']
    })]

    root = graph_operations.get_attribute_schema(input_list, data=True)

    output_tree = generate_output_tree(root)

    assert output_tree == [(0, None, 'attribute'),
                           (1, 'attribute', 'modes', {'bike', 'car', 'walk'})]