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.
Metadata
Release files for py2rdf 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py2rdf-0.1.2.tar.gz | 8.7 kB | Without provenance | Details for py2rdf-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py2rdf-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for py2rdf-0.1.2-py3-none-any.whl |
Total release size: 16.8 kB
Release files / py2rdf-0.1.2.tar.gz
| Download URL | py2rdf-0.1.2.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ce388ae1252c37c8847fa2bec749027b65e71e651f3b3d653c411af4f4b29a3
|
|
BLAKE2b-256 checksum How to use checksums |
04541ec0f12abfd87be996f38b2fdfb19d37bee7ab9ccbc29be3cb8de3e7b7cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / py2rdf-0.1.2-py3-none-any.whl
| Download URL | py2rdf-0.1.2-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8dcdbf7d917e20d2dff80535229ac9e97604caad9318d0ea5feee11c0f67c2bb
|
|
BLAKE2b-256 checksum How to use checksums |
4a8f1c9976ea883075ad743b76b639815d088ef444113e77c7b2d8e8c66481bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for py2rdf 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.