python-dependency-injector/examples/containers/override_declarative.py

29 lines
814 B
Python
Raw Normal View History

2016-05-29 22:06:02 +03:00
"""Declarative IoC container overriding example."""
import dependency_injector.containers as containers
import dependency_injector.providers as providers
2016-05-29 22:06:02 +03:00
class Container(containers.DeclarativeContainer):
"""IoC container."""
2016-05-29 22:06:02 +03:00
sequence_factory = providers.Factory(list)
2016-05-29 22:06:02 +03:00
class OverridingContainer(containers.DeclarativeContainer):
"""Overriding IoC container."""
sequence_factory = providers.Factory(tuple)
2016-05-29 22:06:02 +03:00
# Overriding `Container` with `OverridingContainer`:
Container.override(OverridingContainer)
# Creating some objects using overridden container:
sequence_1 = Container.sequence_factory([1, 2, 3])
sequence_2 = Container.sequence_factory([3, 2, 1])
2016-05-29 22:06:02 +03:00
# Making some asserts:
assert Container.overridden == (OverridingContainer,)
assert sequence_1 == (1, 2, 3) and sequence_2 == (3, 2, 1)