2020-08-21 01:52:12 +00:00
|
|
|
"""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(),
|
|
|
|
)
|
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
|
2020-08-21 01:52:12 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
assert demo_list() == [
|
|
|
|
10,
|
|
|
|
22,
|
|
|
|
service(),
|
|
|
|
42,
|
|
|
|
42,
|
|
|
|
]
|