Skip to main content

Data specifications by type hints

Project description

typespecs

Release Python Downloads DOI Tests

Data specifications by type hints

Installation

pip install typespecs

Basic Usage

from dataclasses import dataclass
from typespecs import ITSELF, Spec, from_annotated
from typing import Annotated as Ann


@dataclass
class Weather:
    temp: Ann[list[float], Spec(category="data", name="Temperature", units="K")]
    wind: Ann[list[float], Spec(category="data", name="Wind speed", units="m/s")]
    loc: Ann[str, Spec(category="metadata", name="Observed location")]


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = from_annotated(weather)
print(specs)
      category              data               name           type units
temp      data  [273.15, 280.15]        Temperature    list[float]     K
wind      data       [5.0, 10.0]         Wind speed    list[float]   m/s
loc   metadata             Tokyo  Observed location  <class 'str'>  <NA>

Advanced Usage

Handling Sub-annotations

Float = Ann[float, Spec(dtype=ITSELF)]


@dataclass
class Weather:
    temp: Ann[list[Float], Spec(category="data", name="Temperature", units="K")]
    wind: Ann[list[Float], Spec(category="data", name="Wind speed", units="m/s")]
    loc: Ann[str, Spec(category="metadata", name="Observed location")]


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = from_annotated(weather)
print(specs)
      category              data            dtype               name           type units
temp      data  [273.15, 280.15]  <class 'float'>        Temperature    list[float]     K
wind      data       [5.0, 10.0]  <class 'float'>         Wind speed    list[float]   m/s
loc   metadata             Tokyo             <NA>  Observed location  <class 'str'>  <NA>

Handling Missing Values

specs = from_annotated(weather, default=None)
print(specs)
      category              data            dtype               name           type units
temp      data  [273.15, 280.15]  <class 'float'>        Temperature    list[float]     K
wind      data       [5.0, 10.0]  <class 'float'>         Wind speed    list[float]   m/s
loc   metadata             Tokyo             None  Observed location  <class 'str'>  None

Handling Full Specification

specs = from_annotated(weather, merge=False)
print(specs)
        category              data            dtype               name             type units
temp        data  [273.15, 280.15]             <NA>        Temperature      list[float]     K
temp/0      <NA>              <NA>  <class 'float'>               <NA>  <class 'float'>  <NA>
wind        data       [5.0, 10.0]             <NA>         Wind speed      list[float]   m/s
wind/0      <NA>              <NA>  <class 'float'>               <NA>  <class 'float'>  <NA>
loc     metadata             Tokyo             <NA>  Observed location    <class 'str'>  <NA>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typespecs-1.0.0.tar.gz (75.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typespecs-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file typespecs-1.0.0.tar.gz.

File metadata

  • Download URL: typespecs-1.0.0.tar.gz
  • Upload date:
  • Size: 75.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typespecs-1.0.0.tar.gz
Algorithm Hash digest
SHA256 eb43eecf65ba32403c6fae947058ea8401df255f8b964809dea719922512a5a1
MD5 4a2fb5a0a68443abc252c3b94dd62c8c
BLAKE2b-256 9cb51a7aab897ebff49c71d732a84c1b536724c2a51302f88491b6b7bb6111eb

See more details on using hashes here.

File details

Details for the file typespecs-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: typespecs-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typespecs-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cb1981debe3beee8595554354a7dd9d75392cfb910435512bf2ed5bc3dec200
MD5 416b30cbdd519fe3b82790de5bc095ce
BLAKE2b-256 d4c4f1353f0bb9546e78a880f99890b6c215a10d543c51107cdcf6665fde5956

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page