A simple dependency injector for Python
Project description
dinjectorr
A simple dependency injector for Python.
Installation
pip install dinjectorr
Usage
Example 1:
from dinjectorr import inject
class A:
def print_hello(self):
print("Hello from A")
class B:
@inject
def __init__(self, a: A):
self._a = a
def print_hello(self):
self._a.print_hello()
b = B()
b.print_hello()
# Output:
# Hello from A
Example 2:
from dinjectorr import Injector
class Client:
def __init__(self, api_key: str):
self._api_key = api_key
def print_api_key(self):
print(self._api_key)
Injector.register(Client, api_key="some_api_key")
client = Injector.get_instance(Client)
client.print_api_key()
# Output:
# some_api_key
Example 3:
from dinjectorr import inject, Injector
class Client:
def __init__(self, api_key: str):
self._api_key = api_key
def print_api_key(self):
print(self._api_key)
class CustomClient(Client):
@inject
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Injector.register(CustomClient, api_key="some_api_key")
custom_client = CustomClient()
custom_client.print_api_key()
# Output:
# some_api_key
Example 4:
from dinjectorr import inject, Injector
class Client:
def __init__(self, api_key: str):
self._api_key = api_key
def print_api_key(self):
print(self._api_key)
class CustomClient(Client):
@inject
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Injector.register(CustomClient, api_key="some_api_key")
custom_client = CustomClient(api_key="another_api_key")
custom_client.print_api_key()
# Output:
# another_api_key
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dinjectorr-0.1.8.tar.gz
(2.9 kB
view hashes)
Built Distribution
Close
Hashes for dinjectorr-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f78d4ecb31a84a4cbba9590f025de8ee7fcc9bcdd333abc5827fb99bc098e1d3 |
|
MD5 | 998003836a1a82b74541f7b8dc2292a8 |
|
BLAKE2b-256 | c7116317ad7f37ee92a7b0754b8a3eec95592a930c690ada62ced43dd784b9ac |