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.

Installation

Install the latest release from PyPI:

pip install py2rdf

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, 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: Literal | str = None
    age: Literal | 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 and serialize
peter = Person(name="Peter", age=30, uri=EX_NS.Peter)
print(peter.rdf())

# Deserialize
from rdflib import Graph
g = Graph()
turtle_data = peter.rdf()
g.parse(data=turtle_data, format="turtle")
peter_copy = Person.deserialize(g, node_uri=EX_NS.Peter)[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.2.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.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py2rdf-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 9ce388ae1252c37c8847fa2bec749027b65e71e651f3b3d653c411af4f4b29a3
MD5 6e1d9b143b6da83f77b41a659d24b90b
BLAKE2b-256 04541ec0f12abfd87be996f38b2fdfb19d37bee7ab9ccbc29be3cb8de3e7b7cf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py2rdf-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8dcdbf7d917e20d2dff80535229ac9e97604caad9318d0ea5feee11c0f67c2bb
MD5 5cfc00e58599de300aeadb43a2941fe2
BLAKE2b-256 4a8f1c9976ea883075ad743b76b639815d088ef444113e77c7b2d8e8c66481bf

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