Skip to main content

A python dependency injection lib

Project description

PyDIT (Python Dependency Injection with Typing)

Translations

Português

Description

With PyDit you can use ABC and Protocol to make interfaces and use the power of Dependency Inversion Principle.

PyDit allow your code to depend only abstract things, not the real implementation.

Install

With poetry

poetry add pydit

With pip

pip install pydit

Usage

Create the PyDit instance:

app/configs/di.py

from pydit import PyDit

pydit = PyDit()

Import the pydit instance and register your project's dependencies:

app/configs/dependencies.py

from typing import Any
from app.configs.di import pydit
from app.adapters.repositories.sqlalchemy.user_repository import SqlalchemyUserRepository
from app.configs.database import get_db_config


def setup_dependencies():
  """
  This is only a suggestion, you're free to configure it like you want
  """

  dependencies: list[dict[str, Any]] = [
    {
      "dependency": SqlalchemyUserRepository,
      "token": "sqlalchemy_user_repository"
    },
    {
      "dependency": get_db_config,
      "token": "database_config",
    },
    {
      "dependency": "HELLO WORLD",
      "token": "test"
    }
  ]

  for dependency in dependencies:
    pydit.add_dependency(dependency["dependency"], dependency.get("token"))

Call the setup_dependencies in the main file

app/main.py

from app.config.di import setup_dependencies()

setup_dependencies()

Injecting a dependency insinde a class

app/domain/user/services/create.py

from typing import cast, Any
from app.configs.di import pydit
# This class can be a Protocol or a clas that inherits from ABC
from app.adapters.repositories.interfaces.user_repositgory import IUserRepository

class CreateUserService:
  @pydit.inject()
  def user_repository(self) -> IUserRepository:
    return cast(IUserRepository, None)

  @pydit.inject(token="test")
  def other_property(self) -> str:
    return ""

  def execute(self, data: dict[str, Any]):
    self.user_repository.create(data)

    # Prints HELLO WORLD
    print(self.other_property)

How you can see, we're depending on the intarface IUserRepository, not the real SqlalchemyUserRepository implementation

Features:

  • Inject values based on type signature
  • Inject values based on inheritance
  • Inject values via token
  • Resolves function dependencies, calling and injecting the call result
  • Inject values in function calls or class constructor __init__ based on the arguments' signatures

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

python_pydit-0.1.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

python_pydit-0.1.3-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file python_pydit-0.1.3.tar.gz.

File metadata

  • Download URL: python_pydit-0.1.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.5 Linux/6.12.9-arch1-1

File hashes

Hashes for python_pydit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c8e773fce4405097ef991f5ddc2547db12d8e75d388a83fd6875db3ddedf7a6e
MD5 18ad78404b06f5f98168a7a0c745dc0d
BLAKE2b-256 192f5fa6392d83e4dce5551c9b089865abfaac46ca7ea0a8bdaf8b6c4e2e2c5e

See more details on using hashes here.

File details

Details for the file python_pydit-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: python_pydit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.5 Linux/6.12.9-arch1-1

File hashes

Hashes for python_pydit-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 026baa74664a5495953fcaf74175f5e1af992e6847239e6efc3be95816626c2e
MD5 c92e04c4610bae89d3569cf552f8612a
BLAKE2b-256 a53fca963c2101f1abe89c7d1e4556d5155af3b986fb9c35104a9873d4693e17

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