Add your description here
Project description
graphty is a Python library for materializing Pydantic object graphs from relational data.
WARNING: This project is in an early stage of development and should be used with caution.
Introduction
The graphty library addresses the structural impedance mismatch between flat relational data representations and hierarchical object models. It extends Pydantic with a small declarative DSL for expressing grouping, aggregation, and deduplication operations. These transformations are compiled into Polars expressions, yielding records that are subsequently validated and materialized as Pydantic model objects.
Although originally developed for implementing typed REST APIs over SPARQL endpoints, the model materializer can be applied to any tabular data representation, including SQL query results, CSV files, dataframes, etc.
Installation
graphty is a PEP 621-compliant package and available on PyPI.
Usage
As mentioned, graphty uses Pydantic model definitions as declarative data transformation instructions, extending Pydantic with a small DSL for grouping and aggregation.
Nested models are resolved recursively, list types are interpreted as aggregation targets and require a group_by definition in ConfigDict.
Basic Example
Given simple relational Author/Work data
data = [
{"name": "Tolkien", "title": "The Hobbit", "year": 1937},
{"name": "Tolkien", "title": "The Lord of the Rings", "year": 1954},
{"name": "Tolkien", "title": "The Silmarillion", "year": 1977},
{"name": "Orwell", "title": "Animal Farm", "year": 1945},
{"name": "Orwell", "title": "1984", "year": 1949},
]
one can define and materialize a Pydantic model like so:
from collections.abc import Iterator
from pydantic import BaseModel
from graphty import ConfigDict, ModelMaterializer
class Work(BaseModel):
title: str
year: int
class Author(BaseModel):
model_config = ConfigDict(group_by="name")
name: str
works: list[Work]
models: Iterator[Author] = ModelMaterializer(model=Author, data=data).generate_models()
Here, the Author model defines a model aggregation target for the Author.works field; the graphty planner will therefore partition the underlying data according to the "name" key and aggregate Work objects into a list.
Note that
graphtyis recursive on all code paths and ergo enables materialization of arbitrarily nested and aggregated object graphs.
The above validates against the Author model and serializes to the following JSON representation:
[
{
"name": "Tolkien",
"works": [
{
"title": "The Hobbit",
"year": 1937
},
{
"title": "The Lord of the Rings",
"year": 1954
},
{
"title": "The Silmarillion",
"year": 1977
}
]
},
{
"name": "Orwell",
"works": [
{
"title": "Animal Farm",
"year": 1945
},
{
"title": "1984",
"year": 1949
}
]
}
]
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
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 graphty-0.3.0.tar.gz.
File metadata
- Download URL: graphty-0.3.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.11","id":"zokor","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6e74657a4ed64f1c6178555a599b475e841786ef7ee54ba2e211bebbe536534
|
|
| MD5 |
5e28a66f71cc97e81dca2df93c9fbcf1
|
|
| BLAKE2b-256 |
5bcaec4256286840d34c77ba98ee84441faa330e07fe230e2eeb22b15e053410
|
File details
Details for the file graphty-0.3.0-py3-none-any.whl.
File metadata
- Download URL: graphty-0.3.0-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.11","id":"zokor","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b33a6b2b2aeb654cdb437addd23a82e2e4ff3924e8c5b83338182e0652c3b329
|
|
| MD5 |
d3dba94c2e752723481810a0474db84f
|
|
| BLAKE2b-256 |
86c11f4f85149dfe2b68b51954bad1032471081c8dcb00de1774032f436bbd9c
|