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

29 lines
925 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
2016-06-03 18:05:47 +03:00
import dependency_injector.providers as providers
import dependency_injector.injections as 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
2016-06-03 18:05:47 +03:00
# Example of using `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
2016-06-03 18:05:47 +03:00
# Example of using `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()