python-dependency-injector/examples/providers/selector.py

35 lines
755 B
Python
Raw Permalink Normal View History

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