Skip to main content

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

Metadata

Release files for asdf-pydantic 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asdf-pydantic 1.1.0
File Size Uploaded Provenance
asdf_pydantic-1.1.0.tar.gz 11.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asdf-pydantic 1.1.0
File Interpreter ABI Platform
asdf_pydantic-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / asdf_pydantic-1.1.0.tar.gz

Download URL asdf_pydantic-1.1.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
79327e8a8e03c9fe56a8746c495c5f5c3533469c6fa38dd46e0ff257ea93f71c
BLAKE2b-256 checksum
How to use checksums
b3c26bc320de73ff340f7c5597fca1bc9cdc96d1e6e68917bf36a66fff9a9f8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / asdf_pydantic-1.1.0-py3-none-any.whl

Download URL asdf_pydantic-1.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be6d0dd3ddb5508273c816ba11d0be4a53919954c4cb9d9c3d5f1799a17cf791
BLAKE2b-256 checksum
How to use checksums
ebc78961d7b3ee8fd8dd25545c8877fbae12106c954f17a0cf287b4cef8f5274
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Security for asdf-pydantic 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page