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().
Metadata
Release files for pydilite 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydilite-0.1.3.tar.gz | 7.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydilite-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.4 kB
Release files / pydilite-0.1.3.tar.gz
| Download URL | pydilite-0.1.3.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d85cf4b0b003736128c4ddb12080155d1c3ebda11ccbd688fa1d711d2b91248d
|
|
BLAKE2b-256 checksum How to use checksums |
3aa3dc114701f3b21e3aa083f04ddc9b496da341768ad0026cf5e4661916b4a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.7 Linux/6.5.0-1025-azure
|
Release files / pydilite-0.1.3-py3-none-any.whl
| Download URL | pydilite-0.1.3-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7063c1bda136697621cfd1b14a18b2d4d0658e081d36dce5008a2fc4d1c017c1
|
|
BLAKE2b-256 checksum How to use checksums |
0570d303e86277ae7c219c355e2a61e4eb64138d8b9b09c3fc42eab64cd549a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.7 Linux/6.5.0-1025-azure
|