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: str = None
age: int = None
mapping: ClassVar[dict] = {
"name": EX_NS.hasName,
"age": EX_NS.hasAge
}
# 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
Release history Release notifications | RSS feed
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.1.tar.gz
(8.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2rdf-0.1.1.tar.gz.
File metadata
- Download URL: py2rdf-0.1.1.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2595eae374337829cc5ff7777b9b3efa4a731f23e36f3fa34f300908f69aeadd
|
|
| MD5 |
dc487929a293b55752bc35d4d457f64d
|
|
| BLAKE2b-256 |
fd37360d9928a7457b27fd8df838f6bb7b1991409cdf0976f9b17cf582c8fae8
|
File details
Details for the file py2rdf-0.1.1-py3-none-any.whl.
File metadata
- Download URL: py2rdf-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa4697e710ed39ca592825f285d7b83598d10c4cbc8df855ad3b6c650ab8d990
|
|
| MD5 |
ad97d494f2b00adebb90d86981bf53cf
|
|
| BLAKE2b-256 |
913b62a87c6a77327f519fb4d7e17f4c955ff15fc4b08d9a83783d40a5ab15cb
|