makeprov.rdfmixin

Functions

fields(class_or_instance)

Return a tuple describing the fields of this dataclass.

get_args(tp)

Get type arguments with all substitutions performed.

get_origin(tp)

Get the unsubscripted version of a type.

get_type_hints(obj[, globalns, localns, ...])

Return type hints for an object.

is_dataclass(obj)

Returns True if obj is a dataclass or an instance of a dataclass.

Classes

Decimal([value, context])

Construct a new Decimal object.

RDFMixin()

Provide JSON-LD serialization helpers for dataclasses.

UUID([hex, bytes, bytes_le, fields, int, ...])

Instances of the UUID class represent UUIDs as specified in RFC 4122.

date

date(year, month, day) --> date object

datetime(year, month, day[, hour[, minute[, ...)

The year, month and day arguments are required.

time

time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object

class makeprov.rdfmixin.RDFMixin

Bases: object

Provide JSON-LD serialization helpers for dataclasses.

The mixin preserves unknown fields when round-tripping JSON-LD documents and offers convenient conversion to rdflib graphs.

Examples

@dataclass
class Person(RDFMixin):
    id: str
    type: str = "ex:Person"
    name: str | None = None

person = Person(id="ex:alice", name="Alice")
jsonld = person.to_jsonld()
classmethod fields_subclass_first()

Return dataclass fields with subclass members ordered first.

Examples

from dataclasses import dataclass

@dataclass
class Thing(RDFMixin):
    id: str

Thing.fields_subclass_first()
classmethod from_jsonld(data)

Deserialize a JSON-LD mapping into the dataclass instance.

Parameters:

data (dict) – Parsed JSON-LD object including optional @context.

Returns:

An instance of cls populated from data.

Return type:

RDFMixin

Examples

person = Person.from_jsonld({"id": "ex:alice", "name": "Alice"})
to_graph()

Convert this object to an rdflib.Graph from JSON-LD.

Returns:

Graph containing triples representing the instance.

Return type:

rdflib.Graph

Raises:

RuntimeError – If rdflib is not installed.

Examples

graph = Person(id="ex:alice").to_graph()
to_jsonld(with_context=True, include_extra=True)

Serialize the object to a JSON-LD-compatible mapping.

Parameters:
  • with_context (bool) – Whether to include the @context section.

  • include_extra (bool) – Whether to emit unknown fields captured during deserialization.

Returns:

JSON-LD representation of the object.

Return type:

dict

Examples

person = Person(id="ex:alice", name="Alice")
payload = person.to_jsonld()