Skip to content

Functions:

import_attr_from_model

import_attr_from_model(model)

Import a Python module's attribute from a model object. Assuming load_py_module_attr was already called on the object.

Parameters:

  • model (ModelBase) –

    Model object containing relevant info for a ModuleAttribute

Returns:

  • Any

    The module attribute, e.g. class or function

Source code in src/rdf_utils/models/python.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def import_attr_from_model(model: ModelBase) -> Any:
    """Import a Python module's attribute from a model object.
    Assuming `load_py_module_attr` was already called on the object.

    Parameters:
        model: Model object containing relevant info for a `ModuleAttribute`

    Returns:
        The module attribute, e.g. class or function
    """
    assert (
        URI_PY_TYPE_MODULE_ATTR in model.types
    ), f"model '{model.id}' doesn't have type '{URI_PY_TYPE_MODULE_ATTR}'"

    module_name = model.get_attr(key=URI_PY_PRED_MODULE_NAME)
    assert module_name is not None, f"module name not loaded for ModuleAttribute '{model.id}'"

    attr_name = model.get_attr(key=URI_PY_PRED_ATTR_NAME)
    assert attr_name is not None, f"attribute name not loaded for ModuleAttribute '{model.id}'"

    return getattr(import_module(module_name), attr_name, None)

import_attr_from_node

import_attr_from_node(graph, uri)

Import a Python module's attribute from an RDF graph using importlib

Parameters:

  • graph (Graph) –

    RDF graph to load relevant info

  • uri (URIRef | str) –

    URI of the ModuleAttribute node

Returns:

  • Any

    The module attribute, e.g. class or function

Source code in src/rdf_utils/models/python.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def import_attr_from_node(graph: Graph, uri: URIRef | str) -> Any:
    """Import a Python module's attribute from an RDF graph using importlib

    Parameters:
        graph: RDF graph to load relevant info
        uri: URI of the `ModuleAttribute` node

    Returns:
        The module attribute, e.g. class or function
    """
    if isinstance(uri, str):
        uri = URIRef(uri)

    module_name = str(graph.value(uri, URI_PY_PRED_MODULE_NAME))
    attr_name = str(graph.value(uri, URI_PY_PRED_ATTR_NAME))

    return getattr(import_module(module_name), attr_name, None)

load_py_module_attr

load_py_module_attr(graph, model, quiet=True, **kwargs)

Load relevant attributes of a ModuleAttribute node into a model object.

Parameters:

  • graph (Graph) –

    RDF graph to load relevant info.

  • model (ModelBase) –

    The model object.

  • quiet (bool, default: True ) –

    If True won't raise an exception

Raises:

  • RuntimeError

    if not quiet and model object does not have ModuleAttribute type

Source code in src/rdf_utils/models/python.py
33
34
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
def load_py_module_attr(graph: Graph, model: ModelBase, quiet: bool = True, **kwargs: Any) -> None:
    """Load relevant attributes of a `ModuleAttribute` node into a model object.

    Parameters:
        graph: RDF graph to load relevant info.
        model: The model object.
        quiet: If True won't raise an exception

    Raises:
        RuntimeError: if not quiet and model object does not have `ModuleAttribute` type
    """
    if URI_PY_TYPE_MODULE_ATTR not in model.types:
        if quiet:
            return
        raise RuntimeError(f"load_py_module_attr: '{model.id}' is not a {URI_PY_TYPE_MODULE_ATTR}")

    module_name = graph.value(model.id, URI_PY_PRED_MODULE_NAME)
    assert (
        module_name is not None
    ), f"ModuleAttribute '{model.id}' doesn't have attr '{URI_PY_PRED_MODULE_NAME}'"
    model.set_attr(key=URI_PY_PRED_MODULE_NAME, val=str(module_name))

    attr_name = graph.value(model.id, URI_PY_PRED_ATTR_NAME)
    assert (
        attr_name is not None
    ), f"ModuleAttribute '{model.id}' doesn't have attr '{URI_PY_PRED_ATTR_NAME}'"
    model.set_attr(key=URI_PY_PRED_ATTR_NAME, val=str(attr_name))