From b5870e9a5743f8fb4f24b5b5c49ee18189cb9f4d Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Tue, 31 Mar 2015 18:00:11 +0300 Subject: [PATCH] Updating examples --- README.md | 34 +++++++++++++++++++---------- examples/readme/inject_decorator.py | 15 ++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 2db46a38..f245fc06 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,20 @@ assert object_a_1.database is object_a_2.database assert object_a_1.get_one() == object_a_2.get_one() == 1 ``` -Also injections could be used by any callable with `@inject` decorator: +### Catalogs + +Catalogs are named set of providers. + +## Advanced usage + +Below you can find some variants of advanced usage of `Objects`. + +### Inject decorator + +`@inject` decorator could be used for patching any callable with injection. +Any Python object will be injected 'as is', except `Objects` providers, +that will be called to provide injectable value. + ```python """`@inject` decorator example.""" @@ -138,25 +151,24 @@ from objects.injections import KwArg from objects.injections import inject -object_a = NewInstance(object) -object_b = NewInstance(object) +new_object = NewInstance(object) -@inject(KwArg('a', object_a)) -@inject(KwArg('b', object_b)) -def example_callback(a, b): +@inject(KwArg('object_a', new_object)) +@inject(KwArg('some_setting', 1334)) +def example_callback(object_a, some_setting): """This function has dependencies on object a and b. Dependencies are injected using `@inject` decorator. """ - assert a is not b - assert isinstance(a, object) - assert isinstance(b, object) + assert isinstance(object_a, object) + assert some_setting == 1334 +example_callback() example_callback() ``` -### Catalogs +### Overriding providers -Catalogs are named set of providers. +### Overriding catalogs diff --git a/examples/readme/inject_decorator.py b/examples/readme/inject_decorator.py index e0143b87..702f2727 100644 --- a/examples/readme/inject_decorator.py +++ b/examples/readme/inject_decorator.py @@ -6,20 +6,19 @@ from objects.injections import KwArg from objects.injections import inject -object_a = NewInstance(object) -object_b = NewInstance(object) +new_object = NewInstance(object) -@inject(KwArg('a', object_a)) -@inject(KwArg('b', object_b)) -def example_callback(a, b): +@inject(KwArg('object_a', new_object)) +@inject(KwArg('some_setting', 1334)) +def example_callback(object_a, some_setting): """This function has dependencies on object a and b. Dependencies are injected using `@inject` decorator. """ - assert a is not b - assert isinstance(a, object) - assert isinstance(b, object) + assert isinstance(object_a, object) + assert some_setting == 1334 example_callback() +example_callback()