Update examples for speech

This commit is contained in:
Roman Mogilatov 2016-06-03 00:47:10 +03:00
parent eb6cd303de
commit da895ea97f
5 changed files with 46 additions and 23 deletions

View File

@ -6,16 +6,21 @@ import dependency_injector.containers as containers
import dependency_injector.providers as providers import dependency_injector.providers as providers
Engine = collections.namedtuple('Engine', [])
Car = collections.namedtuple('Car', ['serial_number', 'engine'])
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
"""IoC container.""" """IoC container."""
engine_factory = providers.Factory(collections.namedtuple('Engine', [])) engine_factory = providers.Factory(Engine)
car_factory = providers.Factory(collections.namedtuple('Car', ['engine']),
engine=engine_factory) car_factory = providers.Factory(Car, engine=engine_factory)
if __name__ == '__main__': if __name__ == '__main__':
car1 = Container.car_factory() car1 = Container.car_factory(serial_number=1)
car2 = Container.car_factory() car2 = Container.car_factory(serial_number=2)
assert car1 is not car2 assert car1.serial_number == 1 and car2.serial_number == 2
assert car1.engine is not car2.engine assert car1.engine is not car2.engine

View File

@ -5,6 +5,7 @@ from dependency_injector import providers
object_factory = providers.Factory(object) object_factory = providers.Factory(object)
if __name__ == '__main__': if __name__ == '__main__':
object1 = object_factory() object1 = object_factory()
object2 = object_factory() object2 = object_factory()

View File

@ -5,13 +5,16 @@ import collections
import dependency_injector.providers as providers import dependency_injector.providers as providers
engine_factory = providers.Factory(collections.namedtuple('Engine', [])) Engine = collections.namedtuple('Engine', [])
car_factory = providers.Factory(collections.namedtuple('Car', ['engine']), Car = collections.namedtuple('Car', ['serial_number', 'engine'])
engine=engine_factory)
engine_factory = providers.Factory(Engine)
car_factory = providers.Factory(Car, engine=engine_factory)
if __name__ == '__main__': if __name__ == '__main__':
car1 = car_factory() car1 = car_factory(serial_number=1)
car2 = car_factory() car2 = car_factory(serial_number=2)
assert car1 is not car2 assert car1.serial_number == 1 and car2.serial_number == 2
assert car1.engine is not car2.engine assert car1.engine is not car2.engine

View File

@ -1,15 +1,24 @@
"""Overriding of factory provider example.""" """Overriding of factory provider example."""
from dependency_injector import providers import collections
import dependency_injector.providers as providers
object_factory = providers.Factory(object) 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))
object_factory.override(providers.Factory(list))
if __name__ == '__main__': if __name__ == '__main__':
object1 = object_factory() car1 = car_factory(serial_number=1)
object2 = object_factory() car2 = car_factory(serial_number=2, engine=Engine())
assert object1 is not object2 assert car1.serial_number == 1 and car2.serial_number == 2
assert isinstance(object1, list) and isinstance(object2, list) assert car1.engine.__class__ is EngineX
assert car2.engine.__class__ is Engine

View File

@ -5,12 +5,17 @@ from container import Container
from dependency_injector.injections import inject from dependency_injector.injections import inject
@inject(car1=Container.car_factory) @inject(car_factory=Container.car_factory.delegate())
@inject(car2=Container.car_factory) @inject(extra_engine=Container.engine_factory)
def main(car1, car2): def main(car_factory, extra_engine):
"""Main function.""" """Main function."""
assert car1 is not car2 car1 = car_factory(serial_number=1)
car2 = car_factory(serial_number=2, engine=extra_engine)
assert car1.serial_number == 1 and car2.serial_number == 2
assert car1.engine is not car2.engine assert car1.engine is not car2.engine
assert car2.engine is extra_engine
if __name__ == '__main__': if __name__ == '__main__':
main() main()