Fast and easy dependency injection framework.
Project description
How to use?
Create an injectable
If you wish to inject a singleton, use singleton decorator.
from injection import singleton
@singleton
class Singleton:
""" class implementation """
If you wish to inject a new instance each time, use injectable decorator.
from injection import injectable
@injectable
class Injectable:
""" 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: Injectable):
""" function implementation """
If inject decorates a class, it will be applied to the __init__ method.
Especially useful for dataclasses:
from dataclasses import dataclass
from injection import inject
@inject
@dataclass
class DataClass:
instance: Injectable = ...
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 singleton
class A:
...
@singleton(reference=A)
class B(A):
...
references parameter example:
from injection import singleton
class A:
...
class B(A):
...
@singleton(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 singleton
@singleton(reference=Singleton)
def my_recipe() -> Singleton:
""" recipe implementation """
Auto inject
By default, injectable and singleton decorators will automatically apply @inject to the decorated class or
function. To disable it, set the auto_inject parameter to False.
from injection import singleton
@singleton(auto_inject=False)
class Singleton:
""" class implementation """
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_injection-0.3.2.tar.gz.
File metadata
- Download URL: python_injection-0.3.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
917a6eba1ce7d261e0f8d5434e88c67d11c6a327cf31853860366d31585945dd
|
|
| MD5 |
1e7f148ddc0a8468a04713d28064456f
|
|
| BLAKE2b-256 |
21e836001b277a43b20d1ce34be89436a394838b69923fd0cbe2ecd46dc790e5
|
File details
Details for the file python_injection-0.3.2-py3-none-any.whl.
File metadata
- Download URL: python_injection-0.3.2-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e21bd38f3520a97f8ba6b70579b222daa42a00c76f0f907b5c45ed4d2af4fbf
|
|
| MD5 |
34c9f2760103e3ed742814476002dd03
|
|
| BLAKE2b-256 |
0085cb6cab3e2026c5d2d9b9d37e0cb1c4c091bb0f5dd96dcab4ad6402ed1fb2
|