Fast and easy dependency injection framework.
Project description
Injection
Fast and easy dependency injection framework.
Quick start
⚠️ Requires Python 3.10 or higher
pip install python-injection
How to use
Create an injectable
If you wish to inject a singleton, use unique
decorator.
from injection import unique
@unique
class MyClass:
""" class implementation """
If you wish to inject a new instance each time, use new
decorator.
from injection import new
@new
class MyClass:
""" class implementation """
Inject an instance
To inject one or several instances, use inject
decorator.
Don't forget to annotate type of parameter to inject.
from injection import inject
@inject
def my_function(instance: MyClass):
""" function implementation """
Inheritance
In the case of inheritance, you can use the decorator parameters reference
or references
to link the injection to
one or several other classes.
Warning: if the child class is in another file, make sure that file is imported before injection.
See load_package
function.
reference
parameter example:
from injection import unique
class A:
...
@unique(reference=A)
class B(A):
...
references
parameter example:
from injection import unique
class A:
...
class B(A):
...
@unique(references=(A, B))
class C(B):
...
Recipes
A recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify the reference class(es) when defining the recipe.
from injection import unique
@unique(reference=MyClass)
def my_recipe() -> MyClass:
""" recipe implementation """
Utils
load_package
Useful for put in memory injectables hidden deep within a package. Example:
package
├── sub_package
│ ├── __init__.py
│ └── module2.py
│ └── class Injectable2
├── __init__.py
└── module1.py
└── class Injectable1
To load Injectable1 and Injectable2 into memory you can do the following:
from injection.utils import load_package
import package
load_package(package)
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file python_injection-0.2.1.tar.gz
.
File metadata
- Download URL: python_injection-0.2.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cb0266cb556ba316c47467b89d70dfae142dcf4beb05cebc6308371c726b5b5 |
|
MD5 | 0a972fa4db6646b2fb092e31afa0db3e |
|
BLAKE2b-256 | 11483d3f20e4c4481758ff5b3dd1643d6c9ecd76210a56a5f62f76574f4f70ee |
File details
Details for the file python_injection-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: python_injection-0.2.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d42367aaa936e3f1c023cd007d9e89e7133b693b3f79728ea72c876812a4f25 |
|
MD5 | 081787de3e865b59af6624235642da5f |
|
BLAKE2b-256 | 1ed7ff9eac1adb5d7c936b85729c0a0d56ae83cad95588dd4a739cbcf029de49 |