Socket
Book a DemoInstallSign in
Socket

pydilite

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pydilite

Python lightweight dependency injection framework

pipPyPI
Version
0.1.3
Maintainers
1

pydilite

license

pydilite is a lightweight dependency injection library for python supporting both sync and async functions.

It is strongly based on ![pythondi]

Installation

pip install pydilite
poetry add pydilite

Usage

Bind classes to the provider

from pydilite import Provider

provider = Provider()
provider.bind(Repo, SQLRepo)
provider.bind(Usecase, CreateUsecase)

After binding, configure the provider to the container

from pydilite import configure, configure_after_clear


# Inject with configure
configure(provider=provider)

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

Define injection

Define the kind of injection you want to use on your clases.

Import inject

from pydilite import inject

Add type annotations that you want to inject dependencies

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

Add decorator

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

Initialize the destination class with no arguments as they are being injected automatically.

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.

Lazy initializing

Using lazy initilization the injected classes will be built when used. It can be used to preinitialize a class with parameters in the constructor.

from pydilite import Provider

provider = Provider()
provider.bind(Repo, SQLRepo, lazy=True)

You can use lazy initializing through lazy option. (default False)

For singleton, use lazy=False.

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

By default, manual injection is lazy. If you want a singleton, instantiate it like repo=SQLRepo().

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts