Skip to main content

Create ASDF tags with pydantic models

Reason this release was yanked:

version issue

Project description

asdf-pydantic

PyPI - Version PyPI - Python Version Documentation Status

Create ASDF tags with pydantic models.

from asdf_pydantic import AsdfPydanticModel

class Rectangle(AsdfPydanticModel):
    _tag = "asdf://asdf-pydantic/examples/tags/rectangle-1.0.0"

    width: float
    height: float

# After creating extension and install ...

af = asdf.AsdfFile()
af["rect"] = Rectangle(width=1, height=1)
#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {
    author: The ASDF Developers,
    homepage: 'http://github.com/asdf-format/asdf',
    name: asdf,
    version: 2.14.3}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension.BuiltinExtension
    software: !core/software-1.0.0 {
        name: asdf,
        version: 2.14.3}
  - !core/extension_metadata-1.0.0 {
    extension_class: mypackage.shapes.ShapesExtension,
    extension_uri: 'asdf://asdf-pydantic/shapes/extensions/shapes-1.0.0'}
rect: !<asdf://asdf-pydantic/shapes/tags/rectangle-1.0.0> {
    height: 1.0,
    width: 1.0}
...

Features

  • Create ASDF tag from your pydantic models with batteries (converters) included.
  • Validates data models as you create them and not only when reading and writing ASDF files.
  • Preserve Python types when deserializing ASDF files.
  • All the cool things that comes with pydantic (e.g., JSON encoder, JSON schema, Pydantic types).
  • Comes with ASDF schemas (TBD).

Installation

pip install asdf-pydantic

Usage

Define your data model with AsdfPydanticModel. For pydantic fans, this has all the features of pydantic's BaseModel.

# mypackage/shapes.py
from asdf_pydantic import AsdfPydanticModel

class Rectangle(AsdfPydanticModel):
    _tag = "asdf://asdf-pydantic/examples/tags/rectangle-1.0.0"

    width: float
    height: float

Then create an extension with the converter included with asdf-pydantic:

# mypackage/extensions.py
from asdf.extension import Extension
from asdf_pydantic.converter import AsdfPydanticConverter
from mypackage.shapes import Rectangle

AsdfPydanticConverter.add_models(Rectangle)

class ShapesExtension(Extension):
    extension_uri = "asdf://asdf-pydantic/examples/extensions/shapes-1.0.0"
    converters = [AsdfPydanticConverter()]
    tags = [*AsdfPydanticConverter().tags]

Install the extension either by entry point specification or add it to asdf.get_config():

import asdf
from mypackage.extensions import ShapeExtension

asdf.get_config().add_extension(ShapesExtension())

af = asdf.AsdfFile()
af["rect"] = Rectangle(width=1, height=1)

with open("shapes.asdf", "wb") as fp:
    af.write_to(fp)

:maxdepth: 1
model
autoapi

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

asdf_pydantic-2.0.0.dev1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

asdf_pydantic-2.0.0.dev1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file asdf_pydantic-2.0.0.dev1.tar.gz.

File metadata

  • Download URL: asdf_pydantic-2.0.0.dev1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for asdf_pydantic-2.0.0.dev1.tar.gz
Algorithm Hash digest
SHA256 e31d0fc69ea919a3ca1078fa2b95a3c9191e7132800748eb23bfce13c768afb9
MD5 9ed0e93362b7af087630f3fc982c95e4
BLAKE2b-256 f98961b67fde503c8fa5e2776a42da57583f6cae1ee928925a6c4a3f30676933

See more details on using hashes here.

File details

Details for the file asdf_pydantic-2.0.0.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for asdf_pydantic-2.0.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 4d2b39a30e7ae1b183b3bb36d342e89c20bea7aca45562d17e1d051293c150b1
MD5 45fdeeabf0344be286fa5de54337c8fb
BLAKE2b-256 0d6634e0283984d96dba3516956ce4947a7b7147f191408ba9627250289afa70

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