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.Resolver
protocol (seesimple_resolver.py
for 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.0.tar.gz
(5.2 kB
view hashes)
Built Distribution
kink-0.1.0-py3-none-any.whl
(6.5 kB
view hashes)