-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.py
41 lines (28 loc) · 938 Bytes
/
provider.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
"""
This example demonstrates how to use a custom provider to populate the configuration.
"""
from minicfg import Field, Minicfg
from minicfg.caster import IntCaster
from minicfg.provider import AbstractProvider
class MockProvider(AbstractProvider):
"""
A custom mock provider that provides the DATABASE_HOST value.
"""
def __init__(self):
self.data = {
"DATABASE_HOST": "localhost",
}
def get(self, key: str) -> str | None:
return self.data.get(key)
class MockConfig(Minicfg):
DATABASE_HOST: str = Field()
DATABASE_PORT: int = Field(default=1234, caster=IntCaster())
mock_provider = MockProvider()
config = MockConfig.populated(mock_provider)
print(f"{config.DATABASE_HOST=}")
print(f"{config.DATABASE_PORT=}")
"""
Try running `python provider.py` and you should see the following output:
>>> config.DATABASE_HOST='localhost'
>>> config.DATABASE_PORT=1234
"""