Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic-kedro-0.5.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

pydantic_kedro-0.5.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-kedro-0.5.0.tar.gz.

File metadata

  • Download URL: pydantic-kedro-0.5.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pydantic-kedro-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2aa69980290c678267181de3271c5a222a174e990b4fca4bd123b5ae10a449d1
MD5 b8c191ccad2d534ee109b6577a25aea5
BLAKE2b-256 6c2d87713a0f5c6ae8022115dd64a1bc3323901f75ae65fcdd8c3ae5eb9b408e

See more details on using hashes here.

File details

Details for the file pydantic_kedro-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_kedro-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d249cd17c18c32d55f35bd5de36eb5e459e8bb396f49b4710a7d8032ea42be4
MD5 6ef2a501ee61a25cc40e50bdacc5ceda
BLAKE2b-256 265454db3b498705ea3c400f358c29d9d7a0879f0f9e3501243d3deef14796f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page