Skip to content

Functions:

  • add_literal_list_pred –

    Add an RDF list of literal values as a predicate value on a subject.

  • add_node_list_pred –

    Add an RDF list of nodes as a predicate value on a subject.

  • load_list_re –

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

add_literal_list_pred

add_literal_list_pred(graph, subject_uri, pred_uri, values)

Add an RDF list of literal values as a predicate value on a subject.

Parameters:

  • graph (Graph) –

    Graph object to add the list to

  • subject_uri (URIRef) –

    Subject node to attach the list to

  • pred_uri (URIRef) –

    Predicate connecting the subject to the list

  • values (tuple[Any, ...] | list[Any]) –

    Values to convert to literals and store in the RDF list

Returns:

Source code in src/rdf_utils/collection.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def add_literal_list_pred(
    graph: Graph, subject_uri: URIRef, pred_uri: URIRef, values: tuple[Any, ...] | list[Any]
) -> Collection:
    """Add an RDF list of literal values as a predicate value on a subject.

    Parameters:
        graph: Graph object to add the list to
        subject_uri: Subject node to attach the list to
        pred_uri: Predicate connecting the subject to the list
        values: Values to convert to literals and store in the RDF list

    Returns:
        Created RDF collection
    """
    literals = []
    for val in values:
        literals.append(Literal(val))
    return add_node_list_pred(
        graph=graph, subject_uri=subject_uri, pred_uri=pred_uri, nodes=literals
    )

add_node_list_pred

add_node_list_pred(graph, subject_uri, pred_uri, nodes)

Add an RDF list of nodes as a predicate value on a subject.

Parameters:

  • graph (Graph) –

    Graph object to add the list to

  • subject_uri (URIRef) –

    Subject node to attach the list to

  • pred_uri (URIRef) –

    Predicate connecting the subject to the list

  • nodes (list[Node]) –

    Nodes to store in the RDF list

Returns:

Source code in src/rdf_utils/collection.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def add_node_list_pred(
    graph: Graph, subject_uri: URIRef, pred_uri: URIRef, nodes: list[Node]
) -> Collection:
    """Add an RDF list of nodes as a predicate value on a subject.

    Parameters:
        graph: Graph object to add the list to
        subject_uri: Subject node to attach the list to
        pred_uri: Predicate connecting the subject to the list
        nodes: Nodes to store in the RDF list

    Returns:
        Created RDF collection
    """
    b = BNode()
    c = Collection(graph=graph, uri=b, seq=nodes)
    graph.add((subject_uri, pred_uri, b))
    return c

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)