๐ฆ A minimal dependency injection library for Python
Project description
๐ฆ DuckDI
DuckDI is a minimal and powerful dependency injection framework for Python, inspired by duck typing and explicit architecture.
It allows you to declare your interfaces, register adapters, and resolve dependencies at runtime using a simple TOML file.
๐ Features
- โ Clean and lightweight API
- โ Zero dependencies
- โ Fully type-safe
- โ Uses TOML to map interfaces to adapters
- โ Clear and informative error messages
- โ
Environment-based configuration (
INJECTIONS_PATH)
๐ฆ Installation
Using Poetry:
poetry add duckdi
Or manually with pip:
pip install duckdi
๐ ๏ธ Usage
1. Define an interface
from duckdi import Interface
@Interface
class IUserRepository:
def get_user(self, user_id: str) -> dict: ...
2. Register an adapter
from duckdi import register
class PostgresUserRepository:
def get_user(self, user_id: str) -> dict:
return {"id": user_id, "name": "John Doe"}
register(PostgresUserRepository)
3. Create your injection payload
Create a file called injections.toml:
[injections]
"user_repository" = "postgres_user_repository"
4. Set the environment variable
You must set the path to the injection file using the INJECTIONS_PATH environment variable:
export INJECTIONS_PATH=./injections.toml
5. Resolve dependencies at runtime
from duckdi import Get
repo = Get(IUserRepository)
user = repo.get_user("123")
print(user) # {'id': '123', 'name': 'John Doe'}
๐ฅ Error Handling
MissingInjectionPayloadError
Raised when no injection payload file is found at the given path.
InvalidAdapterImplementationError
Raised when the registered adapter does not properly implement the expected interface.
๐ Project Structure
duckdi/
โโโ pyproject.toml
โโโ README.md
โโโ src/
โ โโโ duckdi/
โ โโโ duck.py
โ โโโ __init__.py
โ โโโ errors/
โ โ โโโ __init__.py
โ โ โโโ invalid_adapter_implementation_error.py
โ โ โโโ missing_injection_payload_error.py
โ โโโ utils/
โ โโโ __init__.py
โ โโโ buffer_readers.py
โ โโโ serializers.py
โโโ tests/
๐งฉ Advanced Example
You can register multiple adapters and resolve them dynamically:
from duckdi import Interface, register, Get
@Interface
class INotifier:
def send(self, msg: str): ...
class EmailNotifier:
def send(self, msg: str):
print(f"Sending email: {msg}")
register(EmailNotifier)
# injections.toml
# [injections]
# "notifier" = "email_notifier"
notifier = Get(INotifier)
notifier.send("Hello from DuckDI!")
๐ License
Licensed under the MIT License.
See the LICENSE file for more information.
๐ค Author
Developed with โค๏ธ by PhePato
Pull requests, feedback and contributions are welcome!
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 duckdi-0.1.0.tar.gz.
File metadata
- Download URL: duckdi-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.12.0 Linux/6.8.0-57-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e96f00e7487c7e1ac9296cff796968a22a0ddffcf7981ab2ec0d5bd5e71c2dc5
|
|
| MD5 |
1d68adb579ff7f4d14dea785bdbc72c6
|
|
| BLAKE2b-256 |
433bf274a4501c223f600df2d8c1d15cd07816d7e7f7718b10b1164fe42f6619
|
File details
Details for the file duckdi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: duckdi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.12.0 Linux/6.8.0-57-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd1681e3f71f67a9e3e5eabb8212cb1966ca22b566733ee7c0df7d8e26253721
|
|
| MD5 |
ea1745bba63bce05732257c333e2ad59
|
|
| BLAKE2b-256 |
46dfb56d46472f0ed93e0dfe592a01259335dcb4eae618a2baae10cec7e0260e
|