dataspecs.extras.formatting module#

class Format(_format_path: ~typing.Annotated[str | ~os.PathLike[str], <FormatTag.PATH: 2>], _format_attr: ~typing.Annotated[~typing.Literal['path', 'name', 'tags', 'type', 'data', 'anns', 'meta', 'orig'], <FormatTag.ATTR: 1>] = 'data', _format_skipif: ~typing.Annotated[~typing.Any, <FormatTag.SKIPIF: 3>] = None)[source]#

Bases: object

Annotation for formatter specs.

Parameters:
  • _format_path (Annotated[str | PathLike[str], <FormatTag.PATH: 2>]) – Path of data spec(s) to be formatted.

  • _format_attr (Annotated[Literal['path', 'name', 'tags', 'type', 'data', 'anns', 'meta', 'orig'], <FormatTag.ATTR: 1>]) – Name of data spec attribute to be formatted.

  • _format_skipif (Annotated[Any, <FormatTag.SKIPIF: 3>]) – Sentinel value for which formatting is skipped.

format(specs: Specs[TSpec], /, leave: bool = False) Specs[TSpec][source]#

Format data spec attributes by formatter specs.

Parameters:
  • specs (Specs[TSpec]) – Input data specs.

  • leave (bool) – Whether to leave the formatter specs.

Returns:

Data specs whose attributes are formatted.

Return type:

Specs[TSpec]

Examples

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

class Tag(TagBase):
    ATTR = auto()

@dataclass
class Attrs:
    name: Ann[str, Tag.ATTR]
    units: Ann[str, Tag.ATTR]

@dataclass
class Weather:
    temp: Ann[list[float], Attrs("Temperature ({0})", "{0}")]
    units: Ann[str, Format("/temp/(name|units)")] = "degC"

format(from_dataclass(Weather([20.0, 25.0], "K")))
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('/temp/name'),
        name='name',
        tags=(<Tag.ATTR: 1>,),
        type=<class 'str'>,
        data='Temperature (K)', # <- formatted
    ),
    Spec(
        path=Path('/temp/units'),
        name='units',
        tags=(<Tag.ATTR: 1>,),
        type=<class 'str'>, data='K', # <- formatted
    ),
    Spec(
        path=Path('/units'),
        name='units',
        tags=(),
        type=<class 'str'>,
        data='K',
    ),
])