Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/astropenguin/dataspecs

Data specifications by data classes
https://github.com/astropenguin/dataspecs

dataclasses python specifications typing

Last synced: 2 months ago
JSON representation

Data specifications by data classes

Awesome Lists containing this project

README

        

# dataspecs

[![Release](https://img.shields.io/pypi/v/dataspecs?label=Release&color=cornflowerblue&style=flat-square)](https://pypi.org/project/dataspecs/)
[![Python](https://img.shields.io/pypi/pyversions/dataspecs?label=Python&color=cornflowerblue&style=flat-square)](https://pypi.org/project/dataspecs/)
[![Downloads](https://img.shields.io/pypi/dm/dataspecs?label=Downloads&color=cornflowerblue&style=flat-square)](https://pepy.tech/project/dataspecs)
[![DOI](https://img.shields.io/badge/DOI-10.5281/zenodo.10652375-cornflowerblue?style=flat-square)](https://doi.org/10.5281/zenodo.10652375)
[![Tests](https://img.shields.io/github/actions/workflow/status/astropenguin/dataspecs/tests.yaml?label=Tests&style=flat-square)](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')])]
```