Fast and easy dependency injection framework.
Project description
Basic usage
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 """
Note: If the class needs dependencies, these will be resolved when the instance is retrieved.
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 = ...
Note: Doesn't work with Pydantic
BaseModel
because the signature of the__init__
method doesn't contain the dependencies.
Get an instance
Example with get_instance
function:
from injection import get_instance
instance = get_instance(Injectable)
Example with get_lazy_instance
function:
from injection import get_lazy_instance
lazy_instance = get_lazy_instance(Injectable)
# ...
instance = ~lazy_instance
Inheritance
In the case of inheritance, you can use the decorator parameter on
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.
Example with one class:
from injection import singleton
class A:
...
@singleton(on=A)
class B(A):
...
Example with several classes:
from injection import singleton
class A:
...
class B(A):
...
@singleton(on=(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 return type annotation when defining the recipe.
from injection import singleton
@singleton
def my_recipe() -> Singleton:
""" recipe 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
File details
Details for the file python_injection-0.6.6.tar.gz
.
File metadata
- Download URL: python_injection-0.6.6.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05f7557029f8c4dac316f798400e037cddd1845796ca8daabb8cfa940d2c3d05 |
|
MD5 | ab97dcd766a7adf41dad8a9e1d490edb |
|
BLAKE2b-256 | a9e685bca7526b8aebb30c04be2851cc3f7c186c8da1bc40b113cbb875b74a37 |
File details
Details for the file python_injection-0.6.6-py3-none-any.whl
.
File metadata
- Download URL: python_injection-0.6.6-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 822c720ce49fee2c1b7de97dd465b16e0a556da5e6113a4887546e7dfe458d5d |
|
MD5 | 91879ba78d4b5b123f8f7f0d07f1dd2d |
|
BLAKE2b-256 | 262a352eb1ba79111f224bbcef140d81dff18111ff41dc5b756c05de72bfacca |