Skip to content

Functions:

try_expand_curie

try_expand_curie(ns_manager, curie_str, quiet=False)

Execute rdflib expand_curie with exception handling.

Parameters:

  • ns_manager (NamespaceManager) –

    NamespaceManager maps prefixes to namespaces, usually can use the one in the Graph object.

  • curie_str (str) –

    The short URI string to be expanded

  • quiet (bool, default: False ) –

    If False will raise ValueError, else return None

Returns:

Raises:

  • ValueError

    When not quiet and URI cannot be expanded using the given ns_manager

Source code in src/rdf_utils/uri.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def try_expand_curie(
    ns_manager: NamespaceManager, curie_str: str, quiet: bool = False
) -> Optional[URIRef]:
    """Execute rdflib `expand_curie` with exception handling.

    Parameters:
        ns_manager: `NamespaceManager` maps prefixes to namespaces,
                    usually can use the one in the Graph object.
        curie_str: The short URI string to be expanded
        quiet: If False will raise ValueError, else return None

    Returns:
        Expanded URIRef or None

    Raises:
        ValueError: When not `quiet` and URI cannot be expanded using the given `ns_manager`
    """
    try:
        uri = ns_manager.expand_curie(curie_str)

    except ValueError as e:
        if quiet:
            return None

        raise ValueError(f"failed to expand '{curie_str}': {e}")

    return uri

try_parse_n3_iterable

try_parse_n3_iterable(n3_str_iterable, ns_manager, quiet=False)

Parse an iterable of N3 strings using rdflib.util.from_n3 with exception handling.

Parameters:

  • n3_str_iterable (Iterable[str]) –

    Iterable of N3 strings to be parsed.

  • ns_manager (NamespaceManager) –

    NamespaceManager maps prefixes to namespaces.

  • quiet (bool, default: False ) –

    If False will raise ValueError, else return None.

Raises:

  • ValueError

    When not quiet and try_parse_n3_string throws an exception

Source code in src/rdf_utils/uri.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def try_parse_n3_iterable(
    n3_str_iterable: Iterable[str], ns_manager: NamespaceManager, quiet: bool = False
) -> Optional[list[Union[RDFNode, str]]]:
    """Parse an iterable of N3 strings using rdflib.util.from_n3 with exception handling.

    Parameters:
        n3_str_iterable: Iterable of N3 strings to be parsed.
        ns_manager: `NamespaceManager` maps prefixes to namespaces.
        quiet: If False will raise ValueError, else return None.

    Raises:
        ValueError: When not `quiet` and `try_parse_n3_string` throws an exception
    """
    if isinstance(n3_str_iterable, str):
        res = try_parse_n3_string(n3_str=n3_str_iterable, ns_manager=ns_manager, quiet=quiet)
        if res is None:
            return None
        return [res]

    res_list = []
    for n3_str in n3_str_iterable:
        res = try_parse_n3_string(n3_str=n3_str, ns_manager=ns_manager, quiet=quiet)
        if res is None:
            return None

        res_list.append(res)

    return res_list

try_parse_n3_string

try_parse_n3_string(n3_str, ns_manager, quiet=False)

Parse N3 string using rdflib.util.from_n3 with exception handling.

Parameters:

  • n3_str (str) –

    N3 string to be parsed.

  • ns_manager (NamespaceManager) –

    NamespaceManager maps prefixes to namespaces.

  • quiet (bool, default: False ) –

    If False will raise ValueError, else return None.

Raises:

  • ValueError

    When not quiet and from_n3 throws an exception

Source code in src/rdf_utils/uri.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def try_parse_n3_string(
    n3_str: str, ns_manager: NamespaceManager, quiet: bool = False
) -> Optional[Union[RDFNode, str]]:
    """Parse N3 string using rdflib.util.from_n3 with exception handling.

    Parameters:
        n3_str: N3 string to be parsed.
        ns_manager: `NamespaceManager` maps prefixes to namespaces.
        quiet: If False will raise ValueError, else return None.

    Raises:
        ValueError: When not `quiet` and `from_n3` throws an exception
    """
    res = None
    try:
        res = from_n3(s=n3_str, nsm=ns_manager)
    except Exception as e:
        if not quiet:
            raise ValueError(f"Unable to parse N3 string '{n3_str}', got exception: {e}")
    return res