-
Notifications
You must be signed in to change notification settings - Fork 15
/
_value_conversion.py
42 lines (26 loc) · 1.12 KB
/
_value_conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Copyright (c) 2022 Massachusetts Institute of Technology
# SPDX-License-Identifier: MIT
from dataclasses import dataclass, field
from pathlib import Path, PosixPath, WindowsPath
from typing import Any, Callable, Dict, Tuple, Type, cast
from hydra_zen.typing import Builds
from ._utils import get_obj_path
# Some primitive support implemented in _implementations.py
ZEN_VALUE_CONVERSION: Dict[type, Callable[[Any], Any]] = {}
@dataclass
class ConfigComplex:
real: Any
imag: Any
_target_: str = field(default=get_obj_path(complex), init=False)
def convert_complex(value: complex) -> Builds[Type[complex]]:
return cast(Builds[Type[complex]], ConfigComplex(real=value.real, imag=value.imag))
ZEN_VALUE_CONVERSION[complex] = convert_complex
@dataclass
class ConfigPath:
_args_: Tuple[str]
_target_: str = field(default=get_obj_path(Path), init=False)
def convert_path(value: Path) -> Builds[Type[Path]]:
return cast(Builds[Type[Path]], ConfigPath(_args_=(str(value),)))
ZEN_VALUE_CONVERSION[Path] = convert_path
ZEN_VALUE_CONVERSION[PosixPath] = convert_path
ZEN_VALUE_CONVERSION[WindowsPath] = convert_path