-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.py
49 lines (39 loc) · 1.32 KB
/
configs.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
43
44
45
46
47
48
49
import argparse
import dataclasses
from dataclasses import dataclass
from enum import Enum
from typing import Any, Iterable, Tuple, Union, cast, List
from omegaconf import OmegaConf
DataClass = Any
DataClassType = Any
@dataclass
class ConfigBase:
"""Base class that should handle parsing from command line,
json, dicts.
"""
@classmethod
def parse_from_command_line(cls):
return omegaconf_parse(cls)
@classmethod
def parse_from_file(cls, path: str):
oc = OmegaConf.load(path)
return cls.parse_from_dict(OmegaConf.to_container(oc))
@classmethod
def parse_from_command_line_deprecated(cls):
result = DataclassArgParser(
cls, fromfile_prefix_chars="@"
).parse_args_into_dataclasses()
if len(result) > 1:
raise RuntimeError(
f"The following arguments were not recognized: {result[1:]}"
)
return result[0]
@classmethod
def parse_from_dict(cls, inputs):
return DataclassArgParser._populate_dataclass_from_dict(cls, inputs.copy())
@classmethod
def parse_from_flat_dict(cls, inputs):
return DataclassArgParser._populate_dataclass_from_flat_dict(cls, inputs.copy())
def save(self, path: str):
with open(path, "w") as f:
OmegaConf.save(config=self, f=f)