dataspecs
Data specifications by data classes
Installation
pip install dataspecs
Basic usage
Common imports and tags
from dataclasses import dataclass
from dataspecs import TagBase, from_dataclass
from enum import auto
from typing import Annotated as Ann
class Tag(TagBase):
ATTR = auto()
DATA = auto()
DTYPE = auto()
Simple specifications
@dataclass
class Weather:
temp: list[float]
humid: list[float]
location: str
specs = from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
print(specs)
Specs([
Spec(path=Path('/temp'), name='temp', tags=(), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/temp/0'), name='0', tags=(), type=<class 'float'>, data=None),
Spec(path=Path('/humid'), name='humid', tags=(), type=list[float], data=[50.0, 55.0]),
Spec(path=Path('/humid/0'), name='0', tags=(), type=<class 'float'>, data=None),
Spec(path=Path('/location'), name='location', tags=(), type=<class 'str'>, data='Tokyo'),
])
Simple specifications with tags
@dataclass
class Weather:
temp: Ann[list[float], Tag.DATA]
humid: Ann[list[float], Tag.DATA]
location: str
specs = from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
print(specs)
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/temp/0'), name='0', tags=(), type=<class 'float'>, data=None),
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
Spec(path=Path('/humid/0'), name='0', tags=(), type=<class 'float'>, data=None),
Spec(path=Path('/location'), name='location', tags=(), type=<class 'str'>, data='Tokyo'),
])
Nested specifications (with tags)
@dataclass
class Meta:
units: Ann[str, Tag.ATTR]
@dataclass
class Weather:
temp: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA, Meta("K")]
humid: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA, Meta("%")]
location: str
specs = from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
print(specs)
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/temp/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'),
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
Spec(path=Path('/humid/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
Spec(path=Path('/humid/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='%'),
Spec(path=Path('/location'), name='location', tags=(), type=<class 'str'>, data='Tokyo'),
])
Selecting specifications
specs[Tag.DATA]
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
])
specs[Tag]
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/temp/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'),
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
Spec(path=Path('/humid/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
Spec(path=Path('/humid/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='%'),
])
specs[str]
Specs([
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'),
Spec(path=Path('/humid/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='%'),
Spec(path=Path('/location'), name='location', tags=(), type=<class 'str'>, data='Tokyo'),
])
specs["/temp/[a-z]+"]
Specs([
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'),
])
Grouping specifications
specs.groupby("tags")
[
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
]),
Specs([
Spec(path=Path('/temp/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
Spec(path=Path('/humid/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'float'>, data=None),
]),
Specs([
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'),
Spec(path=Path('/humid/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='%'),
]),
Specs([
Spec(path=Path('/location'), name='location', tags=(), type=<class 'str'>, data='Tokyo'),
]),
]
Advanced usage
Formatting specifications
from dataspecs import Format, format
@dataclass
class Meta:
units: Ann[str, Tag.ATTR]
@dataclass
class Weather:
temp: Ann[float, Meta("{0}")]
humid: Ann[float, Meta("{0}")]
temp_units: Ann[str, Format("/temp/units")]
humid_units: Ann[str, Format("/humid/units")]
format(from_dataclass(Weather(20.0, 50.0, "K", "%")))
Specs([
Spec(path=Path('/temp'), name='temp', tags=(), type=<class 'float'>, data=20.0),
Spec(path=Path('/temp/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='K'), # <- data formatted
Spec(path=Path('/humid'), name='humid', tags=(), type=<class 'float'>, data=55.0),
Spec(path=Path('/humid/units'), name='units', tags=(<Tag.ATTR: 1>,), type=<class 'str'>, data='%'), # <- data formatted
Spec(path=Path('/temp_units'), name='temp_units', tags=(), type=<class 'str'>, data='K'),
Spec(path=Path('/humid_units'), name='humid_units', tags=(), type=<class 'str'>, data='%'),
])
Naming specifications
from dataspecs import Name, name
@dataclass
class Weather:
temp: Ann[float, Name("Ground temperature")]
humid: Ann[float, Name("Relative humidity")]
name(from_dataclass(Weather(20.0, 50.0)))
Specs([
Spec(path=Path('/temp'), name='Ground temperature', tags=(), type=<class 'float'>, data=20.0), # <- name replaced
Spec(path=Path('/humid'), name='Relative humidity', tags=(), type=<class 'float'>, data=50.0), # <- name replaced
])
Replacing specifications
from dataspecs import Replace, replace
@dataclass
class Weather:
temp: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA]
humid: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA]
dtype: Ann[type, Replace("/[a-z]+/0", "type")]
replace(from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], int)))
Specs([
Spec(path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0]),
Spec(path=Path('/temp/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'int'>, data=None), # <- type replaced
Spec(path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0]),
Spec(path=Path('/humid/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'int'>, data=None), # <- type replaced
Spec(path=Path('/dtype'), name='dtype', tags=(), type=<class 'type'>, data=<class 'int'>),
])
Specification rules
First union type as representative type
@dataclass
class Weather:
temp: list[int | float] | (int | float)
from_dataclass(Weather(0.0))
Specs([
Spec(path=Path('/temp'), name='temp', tags=(), type=list[int | float], data=0.0),
Spec(path=Path('/temp/0'), name='0', tags=(), type=<class 'int'>, data=None),
])
Metadata
Release files for dataspecs 5.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dataspecs-5.0.0.tar.gz | 50.7 kB | Without provenance | Details for dataspecs-5.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dataspecs-5.0.0-py3-none-any.whl | Python 3 | none | any | Details for dataspecs-5.0.0-py3-none-any.whl |
Total release size: 64.3 kB
Release files / dataspecs-5.0.0.tar.gz
| Download URL | dataspecs-5.0.0.tar.gz |
|---|---|
| Size | 50.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d65500409447f0b728413fef667efe17874db8ed105b354e0196860ca36ae95
|
|
BLAKE2b-256 checksum How to use checksums |
e463abe4dc518eb37cdf2818098b676257431eee672ffe71771d86e0734ed56b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.18
|
Release files / dataspecs-5.0.0-py3-none-any.whl
| Download URL | dataspecs-5.0.0-py3-none-any.whl |
|---|---|
| Size | 13.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5dc1a834fadb944e59c7063c549fc95c7b4041ff74c4b88cb6e464a2af4b95dc
|
|
BLAKE2b-256 checksum How to use checksums |
9d04548cda66ef5c8f2472f0914770714ea339c59c4f05a995e880d662923ebb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.18
|
Security for dataspecs 5.0.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.