Dependency injection for python.
Project description
Kink

Dependency injection made for python
Features
- Easy to use interface
- Extensible with custom dependency resolvers
- Automatic dependency injection
- Lightweight
- Easy to test
Installation
pip install kink
Usage
Simple dependency resolver
from kink import inject
from os import getenv
@inject(dsn=getenv("DB_DSN"), password=getenv("DB_PASSWORD"))
def get_database(dsn: str, password: str):
...
connection = get_database() # Will use `dsn` and `password` from env vars
connection_with_custom_dsn = get_database("my_dsn") # Only `password` will be taken from env vars
connection_with_custom_password = get_database(password="secret")
Nested dependencies resolving
from kink import inject
from os import getenv
@inject(dsn=getenv("DB_DSN"), password=getenv("DB_PASSWORD"))
def get_database_settings(dsn: str, password: str):
...
@inject(db_settings=get_database_settings)
def get_db_connection(db_settings: dict):
...
# This will create partially injected function
@inject(db_connection=get_db_connection)
def get_user(user_id: int, db_connection) -> dict:
...
get_user(12) # will use injected connection, connection will not be established until `get_user` function is called.
mock_connection = ...
get_user(12, mock_connection) # you can easily mock connections
Constructor injection
from kink import inject
def get_connection():
...
class UserRepository:
@inject(db_connection=get_connection)
def __init__(self, unit_of_work, db_connection):
...
def get(self, id: int):
...
Setting dictionary as a resolver
from kink import inject, set_resolver
set_resolver({
"gimme_a": "a",
"gimme_b": "b",
})
@inject()
def print_a_b_c(gimme_a: str, gimme_b: str, gimme_c: str):
print(gimme_a, gimme_b, gimme_c)
print_a_b_c(gimme_c="c") # will print; a, b, c
Defining custom dependency resolver
Kink supports two types of dependency resolvers:
- callables which accepts 3 parameters; property name, property type and context
- classes implementing
kink.resolvers.Resolverprotocol (seesimple_resolver.pyfor example implementation)
from kink import inject, set_resolver
from kink.errors import ResolverError
def resolve_dependency_by_type(param_name: str, param_type: type, context):
if param_type is str:
return "test"
if param_type is int:
return 1
raise ResolverError()
set_resolver(resolve_dependency_by_type)
@inject()
def test_me(one: int, test: str):
print(one, test)
test_me() # will print: 1, "test"
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
kink-0.1.3.tar.gz
(5.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
kink-0.1.3-py3-none-any.whl
(6.7 kB
view details)
File details
Details for the file kink-0.1.3.tar.gz.
File metadata
- Download URL: kink-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.5 Darwin/19.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ce0cabeb31a341576051ccbb1ee710e5b3f741519d0fbe86d867ece10c9c54a
|
|
| MD5 |
9009048823eca1a9d526edb573cb1a66
|
|
| BLAKE2b-256 |
e74a33c8d63aef0134a06954fe01beb574d0cc543729217a12f1fff1e5e9531f
|
File details
Details for the file kink-0.1.3-py3-none-any.whl.
File metadata
- Download URL: kink-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.7.5 Darwin/19.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a30daa614087f9b052b85c6d76f4d18bc0e0da17d3e83292981c4f0f46dc6388
|
|
| MD5 |
5cc94f7eaedb01c7e8d96e17840686cd
|
|
| BLAKE2b-256 |
fc39c47c343dc470af7a556463dee0fed8879915e6d4ea5946c01dc55993f5aa
|