|
| 1 | +# standard library |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Any, Dict, Hashable, Optional, Type |
| 4 | + |
| 5 | + |
| 6 | +# dependencies |
| 7 | +from typing_extensions import Literal, TypeAlias |
| 8 | + |
| 9 | + |
| 10 | +# submodules |
| 11 | +from .typing import Dims, DataClass, AnyDType |
| 12 | + |
| 13 | + |
| 14 | +# type hints |
| 15 | +AnySpec: TypeAlias = "ArraySpec | ScalarSpec" |
| 16 | + |
| 17 | + |
| 18 | +# runtime classes |
| 19 | +@dataclass(frozen=True) |
| 20 | +class ArraySpec: |
| 21 | + """Specification of an array.""" |
| 22 | + |
| 23 | + name: Hashable |
| 24 | + """Name of the array.""" |
| 25 | + |
| 26 | + role: Literal["coord", "data"] |
| 27 | + """Role of the array.""" |
| 28 | + |
| 29 | + default: Any |
| 30 | + """Default value of the array.""" |
| 31 | + |
| 32 | + dims: Dims |
| 33 | + """Dimensions of the array.""" |
| 34 | + |
| 35 | + type: Optional[AnyDType] |
| 36 | + """Data type of the array.""" |
| 37 | + |
| 38 | + origin: Optional[Type[DataClass[Any]]] = None |
| 39 | + """Dataclass of dims and type origins.""" |
| 40 | + |
| 41 | + |
| 42 | +@dataclass(frozen=True) |
| 43 | +class ScalarSpec: |
| 44 | + """Specification of a scalar.""" |
| 45 | + |
| 46 | + name: Hashable |
| 47 | + """Name of the scalar.""" |
| 48 | + |
| 49 | + role: Literal["attr", "name"] |
| 50 | + """Role of the scalar.""" |
| 51 | + |
| 52 | + default: Any |
| 53 | + """Default value of the scalar.""" |
| 54 | + |
| 55 | + type: Any |
| 56 | + """Data type of the scalar.""" |
| 57 | + |
| 58 | + |
| 59 | +class Specs(Dict[str, AnySpec]): |
| 60 | + """Dictionary of any specifications.""" |
| 61 | + |
| 62 | + @property |
| 63 | + def of_attr(self) -> Dict[str, ScalarSpec]: |
| 64 | + """Limit to attribute specifications.""" |
| 65 | + return {k: v for k, v in self.items() if v.role == "attr"} |
| 66 | + |
| 67 | + @property |
| 68 | + def of_coord(self) -> Dict[str, ArraySpec]: |
| 69 | + """Limit to coordinate specifications.""" |
| 70 | + return {k: v for k, v in self.items() if v.role == "coord"} |
| 71 | + |
| 72 | + @property |
| 73 | + def of_data(self) -> Dict[str, ArraySpec]: |
| 74 | + """Limit to data specifications.""" |
| 75 | + return {k: v for k, v in self.items() if v.role == "data"} |
| 76 | + |
| 77 | + @property |
| 78 | + def of_name(self) -> Dict[str, ScalarSpec]: |
| 79 | + """Limit to name specifications.""" |
| 80 | + return {k: v for k, v in self.items() if v.role == "name"} |
0 commit comments