Getting started¶
Installation¶
graphty requires Python 3.12 or newer and is available on PyPI.
Your first model¶
Start with flat, tabular data. Here, every row is a point:
The simplest case is a single model for a point:
from collections.abc import Iterator
from graphty import ModelMaterializer
from pydantic import BaseModel
class Point(BaseModel):
x: float
y: float
points: Iterator[Point] = ModelMaterializer(
model=Point,
data=data,
).generate_models()
This yields one Point per row:
Admittedly, there is not much for graphty to do here yet: the result is the same as calling
Point.model_validate on every row. Things get interesting once models are nested and aggregated.
Nesting and aggregation¶
Now, every row is one vertex of a named polygon:
data = [
{"name": "triangle", "x": 0.0, "y": 0.0},
{"name": "triangle", "x": 1.0, "y": 0.0},
{"name": "triangle", "x": 0.5, "y": 1.0},
{"name": "square", "x": 0.0, "y": 0.0},
{"name": "square", "x": 1.0, "y": 0.0},
{"name": "square", "x": 1.0, "y": 1.0},
{"name": "square", "x": 0.0, "y": 1.0},
]
A polygon is a named list of points. So Point becomes a nested model inside a Polygon model:
from collections.abc import Iterator
from graphty import ConfigDict
class Polygon(BaseModel):
model_config = ConfigDict(group_by="name")
name: str
vertices: list[Point]
polygons: Iterator[Polygon] = ModelMaterializer(
model=Polygon,
data=data,
).generate_models()
The two highlighted lines are all graphty needs:
vertices: list[Point]is an aggregation target: every row of a polygon becomes aPointin this list.Pointis a model itself, sographtybuilds it from the row'sxandycolumns.group_by="name"tellsgraphtywhich column identifies a polygon. Any model with alistfield needs one.
This time, the result is a stream of validated Polygon instances. Serialized to JSON, they look like this:
[
{
"name": "triangle",
"vertices": [
{"x": 0.0, "y": 0.0},
{"x": 1.0, "y": 0.0},
{"x": 0.5, "y": 1.0}
]
},
{
"name": "square",
"vertices": [
{"x": 0.0, "y": 0.0},
{"x": 1.0, "y": 0.0},
{"x": 1.0, "y": 1.0},
{"x": 0.0, "y": 1.0}
]
}
]
Models can nest and aggregate other models to any depth: every code path in graphty is recursive.
graphty's ConfigDict
graphty.ConfigDict is Pydantic's ConfigDict with one extra key, group_by.
Every other Pydantic config option works as usual.