-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathaggregate.py
39 lines (23 loc) · 867 Bytes
/
aggregate.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
"""`Aggregate` provider example."""
from dependency_injector import containers, providers
class ConfigReader:
def __init__(self, path):
self._path = path
def read(self):
print(f"Parsing {self._path} with {self.__class__.__name__}")
...
class YamlReader(ConfigReader):
...
class JsonReader(ConfigReader):
...
class Container(containers.DeclarativeContainer):
config_readers = providers.Aggregate(
yaml=providers.Factory(YamlReader),
json=providers.Factory(JsonReader),
)
if __name__ == "__main__":
container = Container()
yaml_reader = container.config_readers("yaml", "./config.yml")
yaml_reader.read() # Parsing ./config.yml with YamlReader
json_reader = container.config_readers("json", "./config.json")
json_reader.read() # Parsing ./config.json with JsonReader