Python lightweight dependency injection framework
Project description
pydilite
pydilite is a lightweight dependency injection library for python supporting both sync and async functions.
It is strongly based on ![pythondi]
Installation
pip install pydilite
poetry add pydilite
Usage
Bind classes to the provider
from pydilite import Provider
provider = Provider()
provider.bind(Repo, SQLRepo)
provider.bind(Usecase, CreateUsecase)
After binding, configure the provider to the container
from pydilite import configure, configure_after_clear
# Inject with configure
configure(provider=provider)
# Or if you want to fresh inject, use `configure_after_clear`
configure_after_clear(provider=provider)
Define injection
Define the kind of injection you want to use on your clases.
Import inject
from pydilite import inject
Add type annotations that you want to inject dependencies
class Usecase:
def __init__(self, repo: Repo):
self.repo = repo
Add decorator
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
Initialize the destination class with no arguments as they are being injected automatically.
usecase = Usecase()
Or, you can also inject manually through decorator arguments
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
In this case, do not have to configure providers and type annotation.
Lazy initializing
Using lazy initilization the injected classes will be built when used. It can be used to preinitialize a class with parameters in the constructor.
from pydilite import Provider
provider = Provider()
provider.bind(Repo, SQLRepo, lazy=True)
You can use lazy initializing through lazy
option. (default False
)
For singleton, use lazy=False
.
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
By default, manual injection is lazy. If you want a singleton, instantiate it like repo=SQLRepo()
.
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 pydilite-0.1.3.tar.gz
.
File metadata
- Download URL: pydilite-0.1.3.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d85cf4b0b003736128c4ddb12080155d1c3ebda11ccbd688fa1d711d2b91248d |
|
MD5 | ad44131b4f1b44f5bc952438a4724e76 |
|
BLAKE2b-256 | 3aa3dc114701f3b21e3aa083f04ddc9b496da341768ad0026cf5e4661916b4a0 |
File details
Details for the file pydilite-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: pydilite-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7063c1bda136697621cfd1b14a18b2d4d0658e081d36dce5008a2fc4d1c017c1 |
|
MD5 | aee30f3547cfffbb11999e7849a8d214 |
|
BLAKE2b-256 | 0570d303e86277ae7c219c355e2a61e4eb64138d8b9b09c3fc42eab64cd549a6 |