From da895ea97f0b8dc8a0d41ee92cf2a413a8f692a2 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 3 Jun 2016 00:47:10 +0300 Subject: [PATCH] Update examples for speech --- examples/speech/container.py | 17 +++++++++++------ examples/speech/factory.py | 1 + examples/speech/factory_injections.py | 15 +++++++++------ examples/speech/factory_override.py | 23 ++++++++++++++++------- examples/speech/inject.py | 13 +++++++++---- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/examples/speech/container.py b/examples/speech/container.py index 686b0763..e1edbccb 100644 --- a/examples/speech/container.py +++ b/examples/speech/container.py @@ -6,16 +6,21 @@ import dependency_injector.containers as containers import dependency_injector.providers as providers +Engine = collections.namedtuple('Engine', []) +Car = collections.namedtuple('Car', ['serial_number', 'engine']) + + class Container(containers.DeclarativeContainer): """IoC container.""" - engine_factory = providers.Factory(collections.namedtuple('Engine', [])) - car_factory = providers.Factory(collections.namedtuple('Car', ['engine']), - engine=engine_factory) + engine_factory = providers.Factory(Engine) + + car_factory = providers.Factory(Car, engine=engine_factory) + if __name__ == '__main__': - car1 = Container.car_factory() - car2 = Container.car_factory() + car1 = Container.car_factory(serial_number=1) + 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 diff --git a/examples/speech/factory.py b/examples/speech/factory.py index 36915600..d8470898 100644 --- a/examples/speech/factory.py +++ b/examples/speech/factory.py @@ -5,6 +5,7 @@ from dependency_injector import providers object_factory = providers.Factory(object) + if __name__ == '__main__': object1 = object_factory() object2 = object_factory() diff --git a/examples/speech/factory_injections.py b/examples/speech/factory_injections.py index 3394f446..e378b85c 100644 --- a/examples/speech/factory_injections.py +++ b/examples/speech/factory_injections.py @@ -5,13 +5,16 @@ import collections import dependency_injector.providers as providers -engine_factory = providers.Factory(collections.namedtuple('Engine', [])) -car_factory = providers.Factory(collections.namedtuple('Car', ['engine']), - engine=engine_factory) +Engine = collections.namedtuple('Engine', []) +Car = collections.namedtuple('Car', ['serial_number', 'engine']) + +engine_factory = providers.Factory(Engine) +car_factory = providers.Factory(Car, engine=engine_factory) + if __name__ == '__main__': - car1 = car_factory() - car2 = car_factory() + car1 = car_factory(serial_number=1) + 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 diff --git a/examples/speech/factory_override.py b/examples/speech/factory_override.py index ceabc2be..d598979a 100644 --- a/examples/speech/factory_override.py +++ b/examples/speech/factory_override.py @@ -1,15 +1,24 @@ """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__': - object1 = object_factory() - object2 = object_factory() + car1 = car_factory(serial_number=1) + car2 = car_factory(serial_number=2, engine=Engine()) - assert object1 is not object2 - assert isinstance(object1, list) and isinstance(object2, list) + assert car1.serial_number == 1 and car2.serial_number == 2 + assert car1.engine.__class__ is EngineX + assert car2.engine.__class__ is Engine diff --git a/examples/speech/inject.py b/examples/speech/inject.py index 07b64709..963ed799 100644 --- a/examples/speech/inject.py +++ b/examples/speech/inject.py @@ -5,12 +5,17 @@ from container import Container from dependency_injector.injections import inject -@inject(car1=Container.car_factory) -@inject(car2=Container.car_factory) -def main(car1, car2): +@inject(car_factory=Container.car_factory.delegate()) +@inject(extra_engine=Container.engine_factory) +def main(car_factory, extra_engine): """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 car2.engine is extra_engine + if __name__ == '__main__': main()