Skip to main content

๐Ÿฆ† A minimal dependency injection library for Python

Project description

DuckDI Logo

๐Ÿฆ† 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(IUserRepository):
    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(INotifier):
    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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

duckdi-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

duckdi-0.1.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file duckdi-0.1.1.tar.gz.

File metadata

  • Download URL: duckdi-0.1.1.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

Hashes for duckdi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dd16272129f00c83125a3d79d7a8ffc88a2d530e2acd2bea4de18a987be3c493
MD5 a2b12301aebd5e3f040b0e7335e4e7da
BLAKE2b-256 7f1dd5b9605689ef8b69644fcad720dddee173eab3b0494ce02a6abb69abfae6

See more details on using hashes here.

File details

Details for the file duckdi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: duckdi-0.1.1-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

Hashes for duckdi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a409e84e766cdc80901d1911999c00a6c19f19b518a5275a6fdb7a957ab4afc4
MD5 0d08108adb89b78a4a6afe409990e550
BLAKE2b-256 b1d4b07c698080d3057948f1971ce2cb7ca5221df69b8fc06c0579780fde80b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page