Kedro
Project description
pydantic-kedro
Advanced serialization for Pydantic models via Kedro and fsspec.
This package implements custom Kedro "datasets" for both "pure" and "arbitrary" Pydantic models. You can also use it stand-alone, using Kedro just for serializing other object types.
Please see the documentation for a tutorial and more examples.
Usage with Kedro
You can use the [PydanticAutoDataSet][pydantic_kedro.PydanticAutoDataSet]
or any other dataset from pydantic-kedro
within your
Kedro catalog
to save your Pydantic models:
# conf/base/catalog.yml
my_pydantic_model:
type: pydantic_kedro.PydanticAutoDataSet
filepath: folder/my_model
Direct Dataset Usage
This example works for "pure", JSON-safe Pydantic models via
PydanticJsonDataSet
:
from pydantic import BaseModel
from pydantic_kedro import PydanticJsonDataSet
class MyPureModel(BaseModel):
"""Your custom Pydantic model with JSON-safe fields."""
x: int
y: str
obj = MyPureModel(x=1, y="why?")
# Create an in-memory (temporary) file via `fsspec` and save it
ds = PydanticJsonDataSet("memory://temporary-file.json")
ds.save(obj)
# We can re-load it from the same file
read_obj = ds.load()
assert read_obj.x == 1
Standalone Usage
You can also use pydantic-kedro
as a generic saving and loading engine for
Pydantic models:
from tempfile import TemporaryDirectory
from pydantic import BaseModel
from pydantic_kedro import load_model, save_model
class MyModel(BaseModel):
"""My custom model."""
name: str
# We can use any fsspec URL, so we'll make a temporary folder
with TemporaryDirectory() as tmpdir:
save_model(MyModel(name="foo"), f"{tmpdir}/my_model")
obj = load_model(f"{tmpdir}/my_model")
assert obj.name == "foo"
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
File details
Details for the file pydantic-kedro-0.6.2.tar.gz
.
File metadata
- Download URL: pydantic-kedro-0.6.2.tar.gz
- Upload date:
- Size: 30.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cd140f00e1b9eec309362efcab1ccc4dc7b86fc5bff234dfc3b31a8b482890c |
|
MD5 | a514598121efaa7061cc467818d3d10f |
|
BLAKE2b-256 | 66ebf903f5f049a355eec10f91d01e579880245cdcf105b969654f6dab0e7033 |
File details
Details for the file pydantic_kedro-0.6.2-py3-none-any.whl
.
File metadata
- Download URL: pydantic_kedro-0.6.2-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bb37bb276594f5209926d4df2cc360feeea29eb20b57848be8f0c975c379510 |
|
MD5 | aa35f7c5db3b2c01f49d3c45a51ae104 |
|
BLAKE2b-256 | 415b35f397b8d3544151f7cea1702cecca4baad684462d15246c3b6bf55164c5 |