Quickstart

Minimal round-trip in a few lines. For narrative detail, see Getting started.

from triplemodel import TripleModel, rdf_field

FOAF = "http://xmlns.com/foaf/0.1/"

class Person(TripleModel):
    class Rdf:
        namespace = "http://example.org/people/"
        type_uri = f"{FOAF}Person"
        id_field = "slug"

    slug: str
    name: str = rdf_field(f"{FOAF}name")
    age: int | None = rdf_field(f"{FOAF}age", default=None)

alice = Person(slug="alice", name="Alice", age=30)
graph = alice.to_graph()

assert Person.from_graph(graph, alice.subject_uri()) == alice

Serialize to Turtle

alice = Person(slug="alice", name="Alice", age=30)
graph = alice.to_graph()
print(graph.serialize(format="turtle"))

Output:

<http://example.org/people/alice> <http://xmlns.com/foaf/0.1/age> 30 ;
	<http://xmlns.com/foaf/0.1/name> "Alice" ;
	a <http://xmlns.com/foaf/0.1/Person> .

Update an existing graph

Clearing a field requires a sync mode (default to_graph only appends):

from triplemodel import sync_to_graph

alice.age = None
sync_to_graph(alice, graph, mode="replace")

See Updating graphs.

Next: User guides.