-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathdict_non_string_keys.py
64 lines (40 loc) · 1.21 KB
/
dict_non_string_keys.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""`Dict` provider with non-string keys example."""
import dataclasses
from typing import Dict
from dependency_injector import containers, providers
class Command:
...
class CommandA(Command):
...
class CommandB(Command):
...
class Handler:
...
class HandlerA(Handler):
...
class HandlerB(Handler):
...
@dataclasses.dataclass
class Dispatcher:
command_handlers: Dict[Command, Handler]
class Container(containers.DeclarativeContainer):
dispatcher_factory = providers.Factory(
Dispatcher,
command_handlers=providers.Dict({
CommandA: providers.Factory(HandlerA),
CommandB: providers.Factory(HandlerB),
}),
)
if __name__ == "__main__":
container = Container()
dispatcher = container.dispatcher_factory()
assert isinstance(dispatcher.command_handlers, dict)
assert isinstance(dispatcher.command_handlers[CommandA], HandlerA)
assert isinstance(dispatcher.command_handlers[CommandB], HandlerB)
# Call "dispatcher = container.dispatcher_factory()" is equivalent to:
# dispatcher = Dispatcher(
# command_handlers={
# CommandA: HandlerA(),
# CommandB: HandlerB(),
# },
# )