mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-26 19:43:58 +03:00
cf862fe8b5
* Add sketch * Cythonize MethodCaller * Cythonize ItemGetter, AttributeGetter & ProvidedInstance providers * Add docblock for .provided attribute * Refactor repr methods * Add .provided attribute to the Dependency provider * Add tests for the .provided attribute to the majority of the providers * Add docblock for the ProvidedInstance provider * Add docblocks for the rest of the providers * Add example of the provided instance usage * Add tests for provided instance* providers * Add complex provided instance example * Update example provided_instance.py * Add docs
42 lines
903 B
Python
42 lines
903 B
Python
"""Complex example of the injecting of provided instance attributes and items."""
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
class Service:
|
|
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def get_value(self):
|
|
return self.value
|
|
|
|
|
|
service = providers.Singleton(Service, value=42)
|
|
|
|
dependency = providers.Object(
|
|
{
|
|
'foo': {
|
|
'bar': 10,
|
|
'baz': lambda arg: {'arg': arg}
|
|
},
|
|
},
|
|
)
|
|
|
|
demo_list = providers.List(
|
|
dependency.provided['foo']['bar'],
|
|
dependency.provided['foo']['baz'].call(22)['arg'],
|
|
dependency.provided['foo']['baz'].call(service)['arg'],
|
|
dependency.provided['foo']['baz'].call(service)['arg'].value,
|
|
dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(),
|
|
)
|
|
|
|
if __name__ == '__main__':
|
|
assert demo_list() == [
|
|
10,
|
|
22,
|
|
service(),
|
|
42,
|
|
42,
|
|
]
|