https://github.com/astropenguin/dataspecs
Data specifications by data classes
https://github.com/astropenguin/dataspecs
dataclasses python specifications typing
Last synced: 6 months ago
JSON representation
Data specifications by data classes
- Host: GitHub
- URL: https://github.com/astropenguin/dataspecs
- Owner: astropenguin
- License: mit
- Created: 2023-09-18T11:21:49.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-25T12:23:08.000Z (7 months ago)
- Last Synced: 2025-03-25T12:26:35.524Z (7 months ago)
- Topics: dataclasses, python, specifications, typing
- Language: Python
- Homepage: https://astropenguin.github.io/dataspecs/v4.0.0
- Size: 1.67 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# dataspecs
[](https://pypi.org/project/dataspecs/)
[](https://pypi.org/project/dataspecs/)
[](https://pepy.tech/project/dataspecs)
[](https://doi.org/10.5281/zenodo.10652375)
[](https://github.com/astropenguin/dataspecs/actions)Data specifications by data classes
## Installation
```shell
pip install dataspecs==1.0.1
```## Usage
```python
from dataclasses import dataclass
from dataspecs import TagBase, from_dataclass
from enum import auto
from typing import Annotated as Ann
```### Simple specifications
```python
class Tag(TagBase):
ATTR = auto()
DATA = auto()@dataclass
class Weather:
temp: Ann[list[float], Tag.DATA]
humid: Ann[list[float], Tag.DATA]
location: Ann[str, Tag.ATTR]simple_specs = from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
simple_specs
```
```python
Specs([Spec(id=ID('/temp'), tags=(,), type=list[float], data=[20.0, 25.0]),
Spec(id=ID('/humid'), tags=(,), type=list[float], data=[50.0, 55.0]),
Spec(id=ID('/location'), tags=(,), type=, data='Tokyo')])
```### Nested specifications
```python
class Tag(TagBase):
ATTR = auto()
DATA = auto()
DTYPE = auto()
NAME = auto()
UNITS = auto()@dataclass
class Meta:
name: Ann[str, Tag.NAME]
units: Ann[str, Tag.UNITS]@dataclass
class Weather:
temp: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA, Meta("Ground temperature", "K")]
humid: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA, Meta("Relative humidity", "%")]
location: Ann[str, Tag.ATTR]nested_specs = from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
nested_specs
```
```python
Specs([Spec(id=ID('/temp'), tags=(,), type=list[float], data=[20.0, 25.0]),
Spec(id=ID('/temp/0'), tags=(,), type=, data=None),
Spec(id=ID('/temp/name'), tags=(,), type=, data='Ground temperature'),
Spec(id=ID('/temp/units'), tags=(,), type=, data='K'),
Spec(id=ID('/humid'), tags=(,), type=list[float], data=[50.0, 55.0]),
Spec(id=ID('/humid/0'), tags=(,), type=, data=None),
Spec(id=ID('/humid/name'), tags=(,), type=, data='Relative humidity'),
Spec(id=ID('/humid/units'), tags=(,), type=, data='%'),
Spec(id=ID('/location'), tags=(,), type=, data='Tokyo')])
```### Selecting specifications
```python
nested_specs[Tag.DATA]
```
```python
Specs([Spec(id=ID('/temp'), tags=(,), type=list[float], data=[20.0, 25.0]),
Spec(id=ID('/humid'), tags=(,), type=list[float], data=[50.0, 55.0])])
``````python
nested_specs["/temp/[a-z]+"]
```
```python
Specs([Spec(id=ID('/temp/name'), tags=(,), type=, data='Ground temperature'),
Spec(id=ID('/temp/units'), tags=(,), type=, data='K')])
```### Grouping specifications
```python
nested_specs.groups()
```
```python
[Specs([Spec(id=ID('/temp'), tags=(,), type=list[float], data=[20.0, 25.0]),
Spec(id=ID('/temp/0'), tags=(,), type=, data=None),
Spec(id=ID('/temp/name'), tags=(,), type=, data='Ground temperature'),
Spec(id=ID('/temp/units'), tags=(,), type=, data='K')]),
Specs([Spec(id=ID('/humid'), tags=(,), type=list[float], data=[50.0, 55.0]),
Spec(id=ID('/humid/0'), tags=(,), type=, data=None),
Spec(id=ID('/humid/name'), tags=(,), type=, data='Relative humidity'),
Spec(id=ID('/humid/units'), tags=(,), type=, data='%')]),
Specs([Spec(id=ID('/location'), tags=(,), type=, data='Tokyo')])]
```