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 details)
Built Distribution
File details
Details for the file dinjectorr-0.1.8.tar.gz
.
File metadata
- Download URL: dinjectorr-0.1.8.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13ef46324f4a52bb099cbe4e6f434fffd0d821d107fa0aae8d5d2ffb3bcae3c5 |
|
MD5 | 1cb37795cfd2378ed296742704675799 |
|
BLAKE2b-256 | bcdfee716b0e87457b312c51a109cba1aefe9dc87f73551742e5c2d77b0a50f3 |
File details
Details for the file dinjectorr-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: dinjectorr-0.1.8-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f78d4ecb31a84a4cbba9590f025de8ee7fcc9bcdd333abc5827fb99bc098e1d3 |
|
MD5 | 998003836a1a82b74541f7b8dc2292a8 |
|
BLAKE2b-256 | c7116317ad7f37ee92a7b0754b8a3eec95592a930c690ada62ced43dd784b9ac |