mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +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
|
.. literalinclude:: ../../examples/providers/selector.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
: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
|
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
|
a ``Configuration`` provider or any other callable. The ``selector`` callable has to return a
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""`Selector` provider example."""
|
"""`Selector` provider example."""
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
class SomeClass:
|
class SomeClass:
|
||||||
|
@ -11,19 +11,24 @@ class SomeOtherClass:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
selector = providers.Selector(
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
selector = providers.Selector(
|
||||||
config.one_or_another,
|
config.one_or_another,
|
||||||
one=providers.Factory(SomeClass),
|
one=providers.Factory(SomeClass),
|
||||||
another=providers.Factory(SomeOtherClass),
|
another=providers.Factory(SomeOtherClass),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
config.override({'one_or_another': 'one'})
|
container = Container()
|
||||||
instance_1 = selector()
|
|
||||||
|
container.config.override({'one_or_another': 'one'})
|
||||||
|
instance_1 = container.selector()
|
||||||
assert isinstance(instance_1, SomeClass)
|
assert isinstance(instance_1, SomeClass)
|
||||||
|
|
||||||
config.override({'one_or_another': 'another'})
|
container.config.override({'one_or_another': 'another'})
|
||||||
instance_2 = selector()
|
instance_2 = container.selector()
|
||||||
assert isinstance(instance_2, SomeOtherClass)
|
assert isinstance(instance_2, SomeOtherClass)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user