Python-dependency-injection is a simple yet powerful mini-framework for dependency injection in Python
Project description
python-dependency-injection
python-dependency-injection is a simple yet powerful mini-framework for dependency injection in Python
Installation
Use the package manager pip to install Python-dependency-injection.
pip install python-dependency-injection
Description
python-dependency-injection enable to easily inject any python class to another python
upon creation. It supports python 3.6 and above.
It is possible to inject the class as singleton (every injection will provide the same object instance)
or as singlecall (every injection will provide a diffrent object instance)
It is very easy to use all you need to do is put the autowired decorator on the __init__
function of the class you want to inject too (see below)
There are a few requirements of corse:
* You must use python typing hints in the injected __init__ - https://docs.python.org/3/library/typing.html
* The injected class cannot be python builtin types like str
* All injected classes should appear as the last parameters in the __init__ i.e. __init__(non_inject1, non_inject2, inject3: InjectType3, inject4 InjectType4)
If the framework can not inject an object a DependencyInjectionException will be raise specifing the error.
Code
# this class will be injected
from dependency_injection.decorators.autowired import autowired
from dependency_injection.decorators.autowired_enums import AutoWiredType
class InjectedClass:
def __init__(self):
self.message: str = ' from InjectedClass'
def foo(self, str1: str) -> str:
return str1 + self.message
# class objects will be injected to
class TestSingletonInjection:
@autowired()
def __init__(self, my_name: str, injected_class: InjectedClass):
self.injected_class = injected_class
self.my_name = my_name
def test_inject(self) -> str:
return self.injected_class.foo('hello TestClassSingleton')
# class objects will be injected to
class TestSingleCallInjection:
@autowired(AutoWiredType.SINGLECALL)
def __init__(self, my_name: str, injected_class: InjectedClass):
self.injected_class = injected_class
self.my_name = my_name
def test_inject(self) -> str:
return self.injected_class.foo('hello TestClassSinglecall')
if __name__ == '__main__':
test_singleton_injection: TestSingletonInjection = TestSingletonInjection('TestSingletonInjection')
print(test_singleton_injection.test_inject())
test_singlecall_injection: TestSingleCallInjection = TestSingleCallInjection('TestSingleCallInjection')
print(test_singlecall_injection.test_inject())
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
##License MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for python-dependency-injection-0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c0bcb1c77b9d433d28e465ea73fca897c8f8d7c88f7782887683e3bd0fd4116 |
|
MD5 | 00abcf11190bdc9e67ebb39e7e625a57 |
|
BLAKE2b-256 | dd2e113922928e8d33be875431914f6bc7b9cf6fcdec9a51469b7bf46c640c9d |
Close
Hashes for python_dependency_injection-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55d95dc632506629db645dbe7b5ae4bebca0e63f2c708b10e50f625fc690f4d6 |
|
MD5 | 1701e55a1cde157f72f6f8f860fdca53 |
|
BLAKE2b-256 | 6f69aa3f8d46d2cf76ade91048f3e4f9865bb7fbf57b1d15938cf283d574bac8 |