dataspecs.extras.replacing module#

class Replace(_replace_id: ~typing.Annotated[str | ~os.PathLike[str], <Tag.ID: 1>], _replace_of: ~typing.Annotated[~typing.Literal['id', 'tags', 'type', 'data', 'annotations', 'metadata', 'origin'], <Tag.OF: 2>] = 'data', _replace_skipif: ~typing.Annotated[~typing.Any, <Tag.SKIPIF: 3>] = None)[source]#

Bases: object

Annotation for replacer specs.

Parameters:
  • _replace_id (Annotated[str | PathLike[str], <Tag.ID: 1>]) – ID of data spec(s) to be replaced.

  • _replace_of (Annotated[Literal['id', 'tags', 'type', 'data', 'annotations', 'metadata', 'origin'], <Tag.OF: 2>]) – Name of data spec attribute to be replaced.

  • _replace_skipif (Annotated[Any, <Tag.SKIPIF: 3>]) – Sentinel value for which replacing is skipped.

replace(specs: Specs[TSpec], /) Specs[TSpec][source]#

Replace data spec attributes by replacer specs.

Parameters:

specs (Specs[TSpec]) – Input data specs.

Returns:

Data specs whose attributes are replaced.

Return type:

Specs[TSpec]

Examples

from enum import auto
from dataclasses import dataclass
from dataspecs import Replace, TagBase, from_dataclass, replace
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]
    dtype: Ann[type, Replace("/[a-z]+/0", "type")] = None

replace(from_dataclass(Weather([20.0, 25.0], [50.0, 55.0], int)))
Specs([
    Spec(
        id=ID('/temp'),
        tags=(<Tag.DATA: 2>,),
        type=list[float],
        data=[20.0, 25.0],
    ),
    Spec(
        id=ID('/temp/0'),
        tags=(<Tag.DTYPE: 3>,),
        type=<class 'int'>, # <- replaced
        data=None,
    ),
    Spec(
        id=ID('/humid'),
        tags=(<Tag.DATA: 2>,),
        type=list[float],
        data=[50.0, 55.0],
    ),
    Spec(
        id=ID('/humid/0'),
        tags=(<Tag.DTYPE: 3>,),
        type=<class 'int'>, # <- replaced
        data=None,
    ),
    Spec(
        id=ID('/dtype'),
        tags=(),
        type=<class 'type'>,
        data=<class 'int'>,
    ),
    Spec(
        id=ID('/dtype/_replace_id'),
        tags=(<Tag.ID: 1>,),
        type=<class 'str'>,
        data='/[a-z]+/0',
    ),
    Spec(
        id=ID('/dtype/_replace_of'),
        tags=(<Tag.OF: 2>,),
        type=<class 'str'>,
        data='type',
    ),
    Spec(
        id=ID('/dtype/_replace_skipif'),
        tags=(<Tag.SKIPIF: 3>,),
        type=typing.Any,
        data=None,
    ),
])