Skip to main content

Python dependency injection library

Project description

Python Dependency Injection Library

Install

pip3 install pythondi

Usage

  1. Add injection policy
from pythondi import Provider, configure, configure_after_clear


# Init provider
provider = Provider()

# Bind `Impl` class to `Interface` class
provider.bind(Repo, SQLRepo)

# Inject with configure
configure(provider=provider)

# Or if you want to fresh inject, use `configure_after_clear`
configure_after_clear(provider=provider)
  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.

Full Example

import abc

from pythondi import Provider, configure, configure_after_clear, inject


class Repo:
    """Interface class"""
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def get(self):
        pass


class SQLRepo(Repo):
    """Impl class"""
    def __init__(self):
        pass

    def get(self):
        print('SQLRepo')


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


if __name__ == '__main__':
    # Init provider
    provider = Provider()

    # Bind `Impl` class to `Interface` class
    provider.bind(Repo, SQLRepo)

    # Inject with configure
    configure(provider=provider)

    # Or if you want to fresh inject, use `configure_after_clear`
    configure_after_clear(provider=provider)

    # Init class without arguments
    u = Usecase()
    print(u.__dict__)

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.2.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file pythondi-1.0.2.tar.gz.

File metadata

  • Download URL: pythondi-1.0.2.tar.gz
  • Upload date:
  • Size: 2.8 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.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

File hashes

Hashes for pythondi-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a0cbc94f86c186ee3cd4820c5e88245edf813de6f1a5f3c6ea863b3aab80a9b8
MD5 32c260fac4d06f99df086efa52f78235
BLAKE2b-256 8d6cc4ab5f6802317ce3926ed9d2eb5f8ebe96fee250ea2108e758bc6bd7fada

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