mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add usage of the container to the selector example
This commit is contained in:
parent
e48746d65f
commit
d61281a0b9
|
@ -17,7 +17,7 @@ Selector provider
|
|||
.. literalinclude:: ../../examples/providers/selector.py
|
||||
:language: python
|
||||
:lines: 3-
|
||||
:emphasize-lines: 14-18
|
||||
:emphasize-lines: 16-20
|
||||
|
||||
The first argument of the ``Selector`` provider is called ``selector``. It can be an option of
|
||||
a ``Configuration`` provider or any other callable. The ``selector`` callable has to return a
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""`Selector` provider example."""
|
||||
|
||||
from dependency_injector import providers
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
|
||||
class SomeClass:
|
||||
|
@ -11,19 +11,24 @@ class SomeOtherClass:
|
|||
...
|
||||
|
||||
|
||||
config = providers.Configuration()
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
config = providers.Configuration()
|
||||
|
||||
selector = providers.Selector(
|
||||
config.one_or_another,
|
||||
one=providers.Factory(SomeClass),
|
||||
another=providers.Factory(SomeOtherClass),
|
||||
)
|
||||
|
||||
selector = providers.Selector(
|
||||
config.one_or_another,
|
||||
one=providers.Factory(SomeClass),
|
||||
another=providers.Factory(SomeOtherClass),
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
config.override({'one_or_another': 'one'})
|
||||
instance_1 = selector()
|
||||
container = Container()
|
||||
|
||||
container.config.override({'one_or_another': 'one'})
|
||||
instance_1 = container.selector()
|
||||
assert isinstance(instance_1, SomeClass)
|
||||
|
||||
config.override({'one_or_another': 'another'})
|
||||
instance_2 = selector()
|
||||
container.config.override({'one_or_another': 'another'})
|
||||
instance_2 = container.selector()
|
||||
assert isinstance(instance_2, SomeOtherClass)
|
||||
|
|
Loading…
Reference in New Issue
Block a user