2020-06-15 00:32:12 +03:00
|
|
|
"""`List` provider example."""
|
|
|
|
|
|
|
|
import dataclasses
|
|
|
|
from typing import List
|
|
|
|
|
2020-09-04 00:03:28 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-06-15 00:32:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Module:
|
|
|
|
name: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Dispatcher:
|
|
|
|
modules: List[Module]
|
|
|
|
|
|
|
|
|
2020-09-04 00:03:28 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
dispatcher_factory = providers.Factory(
|
|
|
|
Dispatcher,
|
|
|
|
modules=providers.List(
|
2021-09-30 22:32:21 +03:00
|
|
|
providers.Factory(Module, name="m1"),
|
|
|
|
providers.Factory(Module, name="m2"),
|
2020-09-04 00:03:28 +03:00
|
|
|
),
|
|
|
|
)
|
2020-06-15 00:32:12 +03:00
|
|
|
|
2020-09-02 04:39:23 +03:00
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-04 00:03:28 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
dispatcher = container.dispatcher_factory()
|
2020-06-15 00:32:12 +03:00
|
|
|
|
|
|
|
assert isinstance(dispatcher.modules, list)
|
2021-09-30 22:32:21 +03:00
|
|
|
assert dispatcher.modules[0].name == "m1"
|
|
|
|
assert dispatcher.modules[1].name == "m2"
|
2020-06-15 00:32:12 +03:00
|
|
|
|
2020-09-04 00:03:28 +03:00
|
|
|
# Call "dispatcher = container.dispatcher_factory()" is equivalent to:
|
2020-09-02 04:39:23 +03:00
|
|
|
# dispatcher = Dispatcher(
|
|
|
|
# modules=[
|
2021-09-30 22:32:21 +03:00
|
|
|
# Module(name="m1"),
|
|
|
|
# Module(name="m2"),
|
2020-09-02 04:39:23 +03:00
|
|
|
# ],
|
|
|
|
# )
|