-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
50 lines (47 loc) · 1.1 KB
/
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
45
46
47
48
49
50
from catconfig import CatConfig, ValidationError
# Load
# Load config from string
c = CatConfig()
c.load_from_string("""
{
"foo": "bar"
}
""")
# Load config when initalizing CatConfig object
c = CatConfig(data={
'foo': 'bar'
})
# load config from file
c = CatConfig()
c.load_from_file('./tests/assests/test.json')
# Specify config type when initalizing CatConfig object
c = CatConfig(format='json')
c.load_from_file('./tests/assests/test.json')
# Specify config type when loading config file
c = CatConfig()
c.load_from_file('./tests/assests/test.json', format='json')
# Get item
print(c.foo)
# Print: bar
print(bool(c.some.value.does.nt.exists))
# Print: False
print(c['foo'])
# Print: bar
# Validation
# visit https://docs.python-cerberus.org/en/stable/usage.html for more info of schema
schema = {
'foo': {
'type': 'integer'
},
'some_field': {
'type': 'string'
}
}
c = CatConfig(validator_schema=schema)
try:
c.load_from_file('./tests/assests/test.json')
except ValidationError as err:
print(err.message)
# Print:
# arr: unknown field
# foo: must be of integer type