2020-06-29 23:32:12 +03:00
|
|
|
"""`Selector` provider example."""
|
|
|
|
|
2020-09-04 00:38:52 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-06-29 23:32:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
class SomeClass:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class SomeOtherClass:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-09-04 00:38:52 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
config = providers.Configuration()
|
|
|
|
|
|
|
|
selector = providers.Selector(
|
|
|
|
config.one_or_another,
|
|
|
|
one=providers.Factory(SomeClass),
|
|
|
|
another=providers.Factory(SomeOtherClass),
|
|
|
|
)
|
2020-06-29 23:32:12 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-04 00:38:52 +03:00
|
|
|
container = Container()
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
container.config.override({"one_or_another": "one"})
|
2020-09-04 00:38:52 +03:00
|
|
|
instance_1 = container.selector()
|
2020-09-02 04:39:23 +03:00
|
|
|
assert isinstance(instance_1, SomeClass)
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
container.config.override({"one_or_another": "another"})
|
2020-09-04 00:38:52 +03:00
|
|
|
instance_2 = container.selector()
|
2020-09-02 04:39:23 +03:00
|
|
|
assert isinstance(instance_2, SomeOtherClass)
|