python-dependency-injector/examples/speech/factory_override.py

25 lines
712 B
Python
Raw Normal View History

2016-06-02 17:25:45 +00:00
"""Overriding of factory provider example."""
2016-06-02 21:47:10 +00:00
import collections
2016-06-02 17:25:45 +00:00
2016-06-02 21:47:10 +00:00
import dependency_injector.providers as providers
2016-06-02 17:25:45 +00:00
2016-06-02 21:47:10 +00:00
Engine = collections.namedtuple('Engine', [])
Car = collections.namedtuple('Car', ['serial_number', 'engine'])
engine_factory = providers.Factory(Engine)
car_factory = providers.Factory(Car, engine=engine_factory)
EngineX = collections.namedtuple('EngineX', [])
engine_factory.override(providers.Factory(EngineX))
2016-06-02 17:25:45 +00:00
if __name__ == '__main__':
2016-06-02 21:47:10 +00:00
car1 = car_factory(serial_number=1)
car2 = car_factory(serial_number=2, engine=Engine())
2016-06-02 17:25:45 +00:00
2016-06-02 21:47:10 +00:00
assert car1.serial_number == 1 and car2.serial_number == 2
assert car1.engine.__class__ is EngineX
assert car2.engine.__class__ is Engine