-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_example.py
44 lines (35 loc) · 1.43 KB
/
simple_example.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
from config_framework import BaseConfig, VariableKey, Variable
from config_framework.loaders import Dict
loader = Dict.load(
data=dict(
user_id=1,
nested_val=dict(pi=3.14)
)
)
class ConfigSample(BaseConfig):
user_id: Variable[int] = Variable(VariableKey("user_id"))
pi_value: Variable[float] = Variable(VariableKey("nested_val") / "pi")
# Defaults only applied when key isn't found.
# Also default values will be validated after initializing, and after you register new validator.
some_value = Variable("not_found_value", default="Hello world")
@staticmethod
@user_id.register_validator
def validate_user_id(var, value):
# Functions can return bool values or raise
# config_framework.types.custom_exceptions.InvalidValueError
# for more detailed description.
return value == 1
def __post_init__(self) -> None:
print("Post init here!")
print("Values aren't locked yet")
self.new_value = 122
config = ConfigSample(loader)
print("User id:", config.user_id)
print("Pi value:", config.pi_value)
print("Some value:", config.some_value)
print("Post inited value:", config.new_value)
# Configs by default aren't modifiable since frozen=True
# If you need changing variables for modifying config - you must
# create an instance of like this: ConfigSample(frozen=False)
# But right now it will raise NotImplementedError
config.some_value = "random"