Metadata-Version: 2.4
Name: py2rdf
Version: 0.1.2
Summary: A Python library for mapping Python objects to RDF graphs using Pydantic and rdflib.
Author-email: An Lam <an.lam@sintef.no>
License-Expression: MIT
Project-URL: Homepage, https://github.com/SINTEF-9012/py2rdf
Project-URL: Issues, https://github.com/SINTEF-9012/py2rdf/issues
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: rdflib>=7.0.0
Requires-Dist: pydantic>=2.9.2
Requires-Dist: typing-extensions>=4.12.0
Dynamic: license-file

# py2rdf

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

## Installation

Install the latest release from PyPI:

```sh
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

```python
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.
