Skip to main content

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

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 details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file rinject-0.1.2.tar.gz.

File metadata

  • Download URL: rinject-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for rinject-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f0a108b178030557467f3ed90a1f11159bb3017e2088ead8791b5b2ad97bf311
MD5 9ad67c3d98b499043ca4597f108783c0
BLAKE2b-256 f22bbe749951e5bd7ca418d66491c5da4972d3b234711deeea4e7f72cf175c74

See more details on using hashes here.

File details

Details for the file rInject-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: rInject-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for rInject-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2be86df98383e5963c4a6615a49dc551721255d90a5886da2bf2791ff382c32c
MD5 f015e8fe30e1200f1f600281c5423fa8
BLAKE2b-256 4094c8c2bf57c3578ec4294a22ce0c42766266d23352440f301c0d5c652dcad2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page