python-dependency-injector/examples/advanced_usage/inject_simple.py

29 lines
914 B
Python
Raw Normal View History

2015-11-23 20:14:50 +03:00
"""`inject()` decorator simple example."""
2015-08-05 17:01:23 +03:00
2015-11-23 20:14:50 +03:00
from dependency_injector import providers
from dependency_injector import injections
2015-08-05 17:01:23 +03:00
2015-11-23 20:14:50 +03:00
dependency_injector_factory = providers.Factory(object)
2015-08-05 17:01:23 +03:00
# Example of using `di.inject()` decorator keyword argument injections:
2015-11-23 20:14:50 +03:00
@injections.inject(new_object=dependency_injector_factory)
@injections.inject(some_setting=1334)
def example_callback1(new_object, some_setting):
2015-09-01 22:30:49 +03:00
"""Example callback that does some asserts for input args."""
2015-08-05 17:01:23 +03:00
assert isinstance(new_object, object)
assert some_setting == 1334
# Example of using `di.inject()` decorator with positional argument injections:
2015-11-23 20:14:50 +03:00
@injections.inject(dependency_injector_factory, 1334)
def example_callback2(new_object, some_setting):
"""Example callback that does some asserts for input args."""
assert isinstance(new_object, object)
assert some_setting == 1334
example_callback1()
example_callback2()