Skip to main content

A Python library for mapping Python objects to RDF graphs using Pydantic and rdflib.

Project description

py2rdf

A Python library for mapping Python objects to RDF graphs using Pydantic and rdflib.

Features

  • Define RDF models using Python classes and type hints
  • Automatic serialization and deserialization to/from RDF (Turtle, XML, etc.)
  • Support for bidirectional relationships and custom mappings
  • Inheritance and mapping merging for subclassed models
  • Pydantic-based validation and type safety

Usage Example

from py2rdf.rdf_model import RDFModel, URIRefNode, MapTo
from rdflib import Namespace, URIRef
from typing import ClassVar

EX_NS = Namespace("http://example.org/")

class Person(RDFModel):
    CLASS_URI: ClassVar[URIRef] = EX_NS.Person
    name: str = None
    age: int = None
    partner: URIRefNode | "Person" = None
    children: list[URIRefNode | "Person"] = None
    mapping: ClassVar[dict] = {
        "name": EX_NS.hasName,
        "age": EX_NS.hasAge,
        "partner": MapTo(EX_NS.hasPartner, EX_NS.hasPartner),
        "children": MapTo(EX_NS.hasChild, EX_NS.hasParent)
    }

# Create an instance
peter = Person(name="Peter", age=30, uri=EX_NS.Peter)
print(peter.rdf())  # Serialize to RDF (Turtle)

# --- Deserialization Example ---
from rdflib import Graph

turtle_data = peter.rdf()
g = Graph()
g.parse(data=turtle_data, format="turtle")

# Returns a dict of URI string to Person instance
individuals = Person.deserialize(g, node_uri=EX_NS.Peter)
peter_copy = individuals[str(EX_NS.Peter)]
print(peter_copy)

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2rdf-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2rdf-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file py2rdf-0.1.0.tar.gz.

File metadata

  • Download URL: py2rdf-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for py2rdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 96bd59fc098d1f1e60517dcd678a39b0ba86f770c8984596cfc4783a5e3a7cd1
MD5 4271cfb1a4229c294b3cd7724e6913c5
BLAKE2b-256 76faf1fad9e8f9c9ec2719198290de67e392009f8f08bf407dbae15c6e6bc27c

See more details on using hashes here.

File details

Details for the file py2rdf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py2rdf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for py2rdf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56493ae6fbb1b5c5e662ff5955ab1b3d514a3e7c3c8ecfa40d9e395cede7161e
MD5 cdf40fc282981befa68e17f690ce91bc
BLAKE2b-256 f7ddb567e447591554261bf7e4388b6311fedcb7e64ac81309bce89ae78215f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page