Skip to main content

A flexible (annotations or keys) but simple dependency injection library for Python 3.

Project description

rInject

A flexible (annotations or keys) but simple dependency injection library for Python 3.

Installation

pip install rinject

Usage

from rinject import Injector
from datetime import datetime


injector = Injector()


class A:
    def __init__(self, greeting: str):
        self.greeting = greeting

    def __call__(self, name: str):
        print(f"{self.greeting} {name}")


class B:
    def __init__(self, a: A, name: str):
        self.a = a
        self.name = name

    def __call__(self):
        self.a(self.name)


if __name__ == "__main__":
    injector.register_value("greeting", "Hello")
    injector.register_instance(A, A)
    injector.register_instance(B, B)
    injector.register_factory("name", lambda: "Steve@" + datetime.utcnow().isoformat())

    b = injector.resolve(B)
    b()  # outputs => Hello Steve

    b2 = injector.resolve(B)
    assert b is b2

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

rinject-0.1.2.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

rInject-0.1.2-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page