Skip to content

Functions:

  • load_list_re

    Recursively iterate over RDF list containers for extracting lists of lists.

_load_list_re

_load_list_re(graph, first_node, node_set, parse_uri, quiet)

Recursive internal function to extract list of lists from RDF list containers.

Source code in src/rdf_utils/collection.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def _load_list_re(
    graph: Graph, first_node: BNode, node_set: set[IdentifiedNode], parse_uri: bool, quiet: bool
) -> list[Any]:
    """Recursive internal function to extract list of lists from RDF list containers."""
    list_data = []
    for node in graph.items(list=first_node):
        if isinstance(node, URIRef):
            list_data.append(node)
            continue

        if isinstance(node, Literal):
            node_val = node.toPython()
            if not isinstance(node_val, str):
                list_data.append(node_val)
                continue

            if not parse_uri:
                list_data.append(node_val)
                continue

            # try to expand short-form URIs,
            # if doesn't work then just return URIRef of the string
            uri = try_expand_curie(
                ns_manager=graph.namespace_manager, curie_str=node_val, quiet=quiet
            )
            if uri is None:
                uri = URIRef(node_val)

            list_data.append(uri)
            continue

        assert isinstance(
            node, BNode
        ), f"load_collections: node '{node}' not a Literal or BNode, type: {type(node)}"

        if node in node_set:
            raise RuntimeError(f"Loop detected in collection at node: {node}")
        node_set.add(node)

        # recursive call
        list_data.append(_load_list_re(graph, node, node_set, parse_uri, quiet))

    return list_data

load_list_re

load_list_re(graph, first_node, parse_uri=True, quiet=True)

Recursively iterate over RDF list containers for extracting lists of lists.

Parameters:

  • graph (Graph) –

    Graph object to extract the list(s) from

  • first_node (BNode) –

    First element in the list

  • parse_uri (bool, default: True ) –

    if True will try converting literals into URIRef

  • quiet (bool, default: True ) –

    if True will not throw exceptions other than loop detection

Raises:

  • RuntimeError

    When a loop is detected

  • ValueError

    When quiet is False and short URI cannot be expanded

Source code in src/rdf_utils/collection.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def load_list_re(
    graph: Graph, first_node: BNode, parse_uri: bool = True, quiet: bool = True
) -> list[Any]:
    """Recursively iterate over RDF list containers for extracting lists of lists.

    Parameters:
        graph: Graph object to extract the list(s) from
        first_node: First element in the list
        parse_uri: if True will try converting literals into URIRef
        quiet: if True will not throw exceptions other than loop detection

    Raises:
        RuntimeError: When a loop is detected
        ValueError: When `quiet` is `False` and short URI cannot be expanded
    """
    node_set = set()

    return _load_list_re(graph, first_node, node_set, parse_uri, quiet)