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:
    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


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.0.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.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

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

Hashes for duckdi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e96f00e7487c7e1ac9296cff796968a22a0ddffcf7981ab2ec0d5bd5e71c2dc5
MD5 1d68adb579ff7f4d14dea785bdbc72c6
BLAKE2b-256 433bf274a4501c223f600df2d8c1d15cd07816d7e7f7718b10b1164fe42f6619

See more details on using hashes here.

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

Hashes for duckdi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd1681e3f71f67a9e3e5eabb8212cb1966ca22b566733ee7c0df7d8e26253721
MD5 ea1745bba63bce05732257c333e2ad59
BLAKE2b-256 46dfb56d46472f0ed93e0dfe592a01259335dcb4eae618a2baae10cec7e0260e

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