dataspecs.extras.replacing module#

class Replace(id: ~typing.Annotated[str | ~os.PathLike[str], <Tag.ID: 1>], of: ~typing.Annotated[str, <Tag.OF: 2>] = 'data', skipif: ~typing.Annotated[~typing.Any, <Tag.SKIPIF: 3>] = None)[source]#

Bases: object

Annotation for replacer specs.

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

  • of (Annotated[str, <Tag.OF: 2>]) – Name of data spec attribute to be replaced.

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

id: ID: 1>]#

ID of data spec(s) to be replaced.

of: OF: 2>] = 'data'#

Name of data spec attribute to be replaced.

skipif: SKIPIF: 3>] = None#

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('/'),
        tags=(),
        type=<class '__main__.Weather'>,
        data=Weather(temp=[20.0, 25.0], humid=[50.0, 55.0], dtype=<class 'int'>),
    ),
    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'),
        tags=(),
        type=<class 'dataspecs.extras.replacing.Replace'>,
        data=Replace(id='/[a-z]+/0', of='type', skipif=None),
    ),
    Spec(
        id=ID('/dtype/replace/id'),
        tags=(<Tag.ID: 1>,),
        type=<class 'str'>,
        data='/(temp|humid)/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,
    ),
])