dataspecs.extras.replacing module#
- class Replace(_replace_index: ~typing.Annotated[None | str | ~os.PathLike[str] | ~dataspecs.core.typing.TagBase | type[~typing.Any] | slice | ~typing.SupportsIndex, <ReplaceTag.INDEX: 2>], _replace_attr: ~typing.Annotated[~typing.Literal['path', 'name', 'tags', 'type', 'data', 'anns', 'meta', 'orig'], <ReplaceTag.ATTR: 1>] = 'data', _replace_skipif: ~typing.Annotated[~typing.Any, <ReplaceTag.SKIPIF: 3>] = None)[source]#
Bases:
objectAnnotation for replacer specs.
- Parameters:
_replace_index (Annotated[None | str | PathLike[str] | TagBase | type[Any] | slice | SupportsIndex, <ReplaceTag.INDEX: 2>]) – Index of data spec(s) to be replaced.
_replace_attr (Annotated[Literal['path', 'name', 'tags', 'type', 'data', 'anns', 'meta', 'orig'], <ReplaceTag.ATTR: 1>]) – Name of data spec attribute to be replaced.
_replace_skipif (Annotated[Any, <ReplaceTag.SKIPIF: 3>]) – Sentinel value for which replacing is skipped.
- class ReplaceTag(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
TagBaseCollection of tags for replacer specs.
- ATTR = 1#
Tag for name of data spec attribute to be replaced.
- INDEX = 2#
Tag for index of data spec(s) to be replaced.
- SKIPIF = 3#
Tag for sentinel value for which replacing is skipped.
- replace(specs: Specs[TSpec], /, *, leave: bool = False) Specs[TSpec][source]#
Replace data spec attributes by replacer specs.
- Parameters:
specs (Specs[TSpec]) – Input data specs.
leave (bool) – Whether to leave the replacer 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( path=Path('/temp'), name='temp', tags=(<Tag.DATA: 2>,), type=list[float], data=[20.0, 25.0], ), Spec( path=Path('/temp/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'int'>, # <- replaced data=None, ), Spec( path=Path('/humid'), name='humid', tags=(<Tag.DATA: 2>,), type=list[float], data=[50.0, 55.0], ), Spec( path=Path('/humid/0'), name='0', tags=(<Tag.DTYPE: 3>,), type=<class 'int'>, # <- replaced data=None, ), Spec( path=Path('/dtype'), name='dtype', tags=(), type=<class 'type'>, data=<class 'int'>, ), ])