Create ASDF tags with pydantic models
Project description
asdf-pydantic
:::{tip} For v1, see https://asdf-pydantic.readthedocs.io/en/v1/ :::
Type-validated scientific data serialization with ASDF and Pydantic models
import asdf
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 File
print(af.dumps())
#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}
...
ASDF Schema
print(af["rect"].model_asdf_schema())
%YAML 1.1
---
$schema: http://stsci.edu/schemas/asdf/asdf-schema-1.0.0
id: asdf://asdf-pydantic/examples/tags/rectangle-1.0.0/schema
title: Rectangle
type: object
properties:
width:
title: Width
type: number
height:
title: Height
type: number
required:
- width
- height
JSON Schema
print(af["rect"].model_json_schema())
{
"properties": {
"width": {
"title": "Width",
"type": "number"
},
"height": {
"title": "Height",
"type": "number"
}
},
"required": [
"width",
"height"
],
"title": "Rectangle",
"type": "object"
}
Features
- Create ASDF tag from your pydantic models with batteries (converters) included
- Automatically generate ASDF schemas
- Validate data models as you create them, and not only when reading and writing ASDF files
- Preserve Python types when deserializing ASDF files
- All the benefits of pydantic (e.g., JSON encoder, JSON schema, pydantic types).
Installation
pip install "asdf-pydantic>=2a"
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: Annotated[
u.Quantity[u.m], AsdfTag("tag:stsci.edu:asdf/core/unit/quantity-1.*")
]
height: Annotated[
u.Quantity[u.m], AsdfTag("tag:stsci.edu:asdf/core/unit/quantity-1.*")
]
Then create an ASDF extension with the help of the provided converter class AsdfPydanticConverter.
# 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]
After your extension is installed with either the entrypoint method or temporarily
with 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)
# Write
af.write_to("shapes.asdf")
# Read back and validate
with asdf.open("shapes.asdf", "rb") as af:
print(af["rect"])
Read Further
:maxdepth: 1
concepts/index
tutorials/index
apidocs/index
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 Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asdf_pydantic-2.0.0a5.tar.gz.
File metadata
- Download URL: asdf_pydantic-2.0.0a5.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af249e45a2637bc5201bf85eccc75713aa846ca93464322076d0f3615e553faf
|
|
| MD5 |
7c2f3f64d828112f53d203f5cca54acc
|
|
| BLAKE2b-256 |
8bcb8a3dd830a4e8c59a89d0a64f3b695550b2a3ae9c6b5d8501e62ea39c4830
|
Provenance
The following attestation bundles were made for asdf_pydantic-2.0.0a5.tar.gz:
Publisher:
release.yml on ketozhang/asdf-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asdf_pydantic-2.0.0a5.tar.gz -
Subject digest:
af249e45a2637bc5201bf85eccc75713aa846ca93464322076d0f3615e553faf - Sigstore transparency entry: 480839701
- Sigstore integration time:
-
Permalink:
ketozhang/asdf-pydantic@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Branch / Tag:
refs/tags/v2.0.0.a5 - Owner: https://github.com/ketozhang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Trigger Event:
push
-
Statement type:
File details
Details for the file asdf_pydantic-2.0a5-py3-none-any.whl.
File metadata
- Download URL: asdf_pydantic-2.0a5-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
954c064a62a1e18204caf91e175d03a28a34ca5031830b1e297f45e216af4757
|
|
| MD5 |
c23e172435faf2f3bbb440bd1902ccfe
|
|
| BLAKE2b-256 |
eb11246cda2eaf0a3410e4879aa8dcf081671f197d3f8b99d0341a846782247b
|
Provenance
The following attestation bundles were made for asdf_pydantic-2.0a5-py3-none-any.whl:
Publisher:
release.yml on ketozhang/asdf-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asdf_pydantic-2.0a5-py3-none-any.whl -
Subject digest:
954c064a62a1e18204caf91e175d03a28a34ca5031830b1e297f45e216af4757 - Sigstore transparency entry: 482807241
- Sigstore integration time:
-
Permalink:
ketozhang/asdf-pydantic@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Branch / Tag:
refs/tags/v2.0a5 - Owner: https://github.com/ketozhang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Trigger Event:
push
-
Statement type:
File details
Details for the file asdf_pydantic-2.0.0a5-py3-none-any.whl.
File metadata
- Download URL: asdf_pydantic-2.0.0a5-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3c4b36fd87221e0a7bc7afb787937c9983bcef52c631ed036fec65af17483a6
|
|
| MD5 |
4b83ac46b0eb7297880052b6d9555aa3
|
|
| BLAKE2b-256 |
86214cd85040de138ce2c7f3691800198ae568b84ab2ab7818504acd4d23afbe
|
Provenance
The following attestation bundles were made for asdf_pydantic-2.0.0a5-py3-none-any.whl:
Publisher:
release.yml on ketozhang/asdf-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asdf_pydantic-2.0.0a5-py3-none-any.whl -
Subject digest:
d3c4b36fd87221e0a7bc7afb787937c9983bcef52c631ed036fec65af17483a6 - Sigstore transparency entry: 480839715
- Sigstore integration time:
-
Permalink:
ketozhang/asdf-pydantic@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Branch / Tag:
refs/tags/v2.0.0.a5 - Owner: https://github.com/ketozhang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7eafff6bafed7c126d2ce64d3c5d34323b0d228d -
Trigger Event:
push
-
Statement type: