dataspecs.core.api module#

from_dataclass(obj: DataClassObject | type[DataClassObject], /, *, path: str | PathLike[str] = ROOT) Specs[Spec[Any]][source]#
from_dataclass(obj: DataClassObject | type[DataClassObject], /, *, factory: Callable[[...], TSpec], path: str | PathLike[str] = ROOT) Specs[TSpec]

Create data specs from a dataclass (object).

Parameters:
  • obj – Dataclass (object) to be parsed.

  • factory – Factory for creating each data spec.

  • path – Path of the parent data spec.

Returns:

Data specs created from the dataclass (object).

Examples

from enum import auto
from dataclasses import dataclass
from dataspecs import TagBase, from_dataclass
from typing import Annotated as Ann

class Tag(TagBase):
    ATTR = auto()
    DATA = auto()
    DTYPE = auto()

@dataclass
class Weather:
    temp: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA]
    humid: Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA]
    location: Ann[str, Tag.ATTR]

from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], "Tokyo"))
Specs([
    Spec(
        path=Path('/temp'),
        tags=(<Tag.DATA: 2>,),
        type=list[float],
        data=[20.0, 25.0],
    ),
    Spec(
        path=Path('/temp/0'),
        tags=(<Tag.DTYPE: 3>,),
        type=<class 'float'>,
        data=None,
    ),
    Spec(
        path=Path('/humid'),
        tags=(<Tag.DATA: 2>,),
        type=list[float],
        data=[50.0, 55.0],
    ),
    Spec(
        path=Path('/humid/0'),
        tags=(<Tag.DTYPE: 3>,),
        type=<class 'float'>,
        data=None,
    ),
    Spec(
        path=Path('/location'),
        tags=(<Tag.ATTR: 1>,),
        type=<class 'str'>,
        data='Tokyo',
    ),
])
from_typehint(obj: Any, /, *, path: str | PathLike[str] = ROOT, data: Any = MISSING, meta: dict[str, Any] | None = None, orig: Any | None = None) Specs[Spec[Any]][source]#
from_typehint(obj: Any, /, *, factory: Callable[[...], TSpec], path: str | PathLike[str] = ROOT, data: Any = MISSING, meta: dict[str, Any] | None = None, orig: Any | None = None) Specs[TSpec]

Create data specs from a type hint.

Parameters:
  • obj – Type hint to be parsed.

  • factory – Factory for creating each data spec.

  • path – Path of the parent data spec.

  • data – Data of the parent data spec.

  • meta – Metadata of the parent data spec.

  • orig – Origin of the parent data spec.

Returns:

Data specs created from the type hint.

Examples

from enum import auto
from dataspecs import TagBase, from_typehint
from typing import Annotated as Ann

class Tag(TagBase):
    DATA = auto()
    DTYPE = auto()

from_typehint(Ann[list[Ann[float, Tag.DTYPE]], Tag.DATA])
Specs([
    Spec(
        path=Path('/'),
        tags=(<Tag.DATA: 1>,),
        type=list[float],
        data=None,
    ),
    Spec(
        path=Path('/0'),
        tags=(<Tag.DTYPE: 2>,),
        type=<class 'float'>,
        data=None,
    ),
])