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.2.tar.gz (5.4 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.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: duckdi-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.0 Linux/6.8.0-59-generic

File hashes

Hashes for duckdi-0.1.2.tar.gz
Algorithm Hash digest
SHA256 18e37a6f719fa2cb469b4137b3a69e6598d9917bb7b7b3dcb05af9c787e321d3
MD5 f24d4194bdf88515833ff330d38214e9
BLAKE2b-256 04f3be740549bcb5bc11831fa42cd735b558240e2591935a2879b12b2c38781a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: duckdi-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.0 Linux/6.8.0-59-generic

File hashes

Hashes for duckdi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe3dfbb36e14c266f6ba1c667a7ee0e1906edb92b855cbe33165032d45dc5769
MD5 50cff7f11340164c182ebf03aa85262f
BLAKE2b-256 6fc38d19d7d17fa3ab79c2f9578d63489c981a2ff9502db839a824e65de5be44

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