Skip to main content

Python dependency injection library

Project description

Python Dependency Injection Library

Usage

  1. Add injection policy
from pythondi import Provider, configure


class DI:
    def __init__(self):
        self.provider = Provider()

    def bind_to_provider(self) -> None:
        # Inject `Impl` class to `Interface` class
        self.provider.bind(Repo, SQLRepo)

    def bind_and_configure(self) -> None:
        # Binding provider
        self.bind_to_provider()

        # Configure injector
        configure(provider=self.provider)

        # Configure injector after clear provider
        configure_after_clear(provider=self.provider)


if __name__ == '__main__':
    di = DI()
    di.bind_and_configure()
  1. Import inject
from pythondi import inject
  1. Add type annotations that you want to inject dependencies
class Usecase:
    def __init__(self, repo: Repo):
        self.repo = repo
  1. Add decorator
class Usecase:
    @inject()
    def __init__(self, repo: Repo):
        self.repo = repo
  1. Initialize class with no arguments
usecase = Usecase()

Or, you can also inject manually through decorator arguments

class Usecase:
    @inject(repo=SQLRepo)
    def __init__(self, repo):
        self.repo = repo

In this case, do not have to configure providers and type annotation.

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

PythonDI-1.0.0.tar.gz (2.1 kB view details)

Uploaded Source

File details

Details for the file PythonDI-1.0.0.tar.gz.

File metadata

  • Download URL: PythonDI-1.0.0.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.4

File hashes

Hashes for PythonDI-1.0.0.tar.gz
Algorithm Hash digest
SHA256 848eabaf8dbeaaca2da9d364c06c12d6f527d4c9d652fcc96fab132bc46a9d40
MD5 1d27d3f7e0bcfccee678b9e65ee6eafa
BLAKE2b-256 9f42c4330ea8e31eebb6842fd803850bef7054c2e1cb1c6071f94ab7aeb8e280

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