Create ASDF tags with pydantic models
Project description
asdf-pydantic
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
converter = AsdfPydanticConverter()
converter.add_models(Rectangle)
class ShapesExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/shapes-1.0.0"
converters = [converter]
tags = [*converter.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
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 asdf_pydantic-2.0.0a4.tar.gz
.
File metadata
- Download URL: asdf_pydantic-2.0.0a4.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b51e2442415752d32e6c94543c721c882dd4494ecf2451f70ac73c1b8445d3df |
|
MD5 | cac11f95e551ef00064caa01bd4569f6 |
|
BLAKE2b-256 | 20b52434ddd297656303c97b67c8f92c4687feac1321c3b402cf4d6341b38278 |
Provenance
The following attestation bundles were made for asdf_pydantic-2.0.0a4.tar.gz
:
Publisher:
release.yml
on ketozhang/asdf-pydantic
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
asdf_pydantic-2.0.0a4.tar.gz
- Subject digest:
b51e2442415752d32e6c94543c721c882dd4494ecf2451f70ac73c1b8445d3df
- Sigstore transparency entry: 149619297
- Sigstore integration time:
- Predicate type:
File details
Details for the file asdf_pydantic-2.0.0a4-py3-none-any.whl
.
File metadata
- Download URL: asdf_pydantic-2.0.0a4-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b09548fb84061b1d44c9a7598f5f9d1da1faf22ca5279d40a68d2a864c708f83 |
|
MD5 | d1144749daa3e8707a2d6cb22c57f40b |
|
BLAKE2b-256 | 1c76d260f6b36bed915efb92068da235fee45d1fc1e298fc646c4c2652445be0 |
Provenance
The following attestation bundles were made for asdf_pydantic-2.0.0a4-py3-none-any.whl
:
Publisher:
release.yml
on ketozhang/asdf-pydantic
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
asdf_pydantic-2.0.0a4-py3-none-any.whl
- Subject digest:
b09548fb84061b1d44c9a7598f5f9d1da1faf22ca5279d40a68d2a864c708f83
- Sigstore transparency entry: 149619298
- Sigstore integration time:
- Predicate type: