-
-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathprovided_instance_complex.py
46 lines (33 loc) · 1.05 KB
/
provided_instance_complex.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
"""Complex example of the injecting of provided instance attributes and items."""
from dependency_injector import containers, providers
class Service:
def __init__(self, value):
self.value = value
def get_value(self):
return self.value
class Container(containers.DeclarativeContainer):
service = providers.Singleton(Service, value=42)
dependency = providers.Object(
{
"foo": {
"bar": 10,
"baz": lambda arg: {"arg": arg}
},
},
)
demo_list = providers.List(
dependency.provided["foo"]["bar"],
dependency.provided["foo"]["baz"].call(22)["arg"],
dependency.provided["foo"]["baz"].call(service)["arg"],
dependency.provided["foo"]["baz"].call(service)["arg"].value,
dependency.provided["foo"]["baz"].call(service)["arg"].get_value.call(),
)
if __name__ == "__main__":
container = Container()
assert container.demo_list() == [
10,
22,
container.service(),
42,
42,
]