Skip to main content

Dependency injection for python.

Project description

Kink PyPI Linting and Tests codecov

Dependency injection made for python

Features

  • Easy to use interface
  • Extensible with custom dependency resolvers
  • Automatic dependency injection
  • Lightweight
  • Support for async with asyncio

Installation

pip install kink

Usage

Adding service to dependency injection container

Dependency container is a dict-like object, adding new service to dependency container is as simple as the following example:

from kink import di
from os import getenv

di["db_name"] = getenv("DB_NAME")
di["db_password"] = getenv("DB_PASSWORD")

Adding service factory to dependency injection container

Kink also supports on-demand service creation. In order to define such a service, lambda function should be used:

from kink import di
from sqlite3 import connect

di["db_connection"] = lambda di: connect(di["db_name"])

In this scenario connection to database will not be established until service is requested.

Adding factorised services to dependency injection

Factorised services are services that are instantiated every time they are requested.

from kink import di
from sqlite3 import connect

di.factories["db_connection"] = lambda di: connect(di["db_name"])

connection_1 = di["db_connection"]
connection_2 = di["db_connection"]

connection_1 != connection_2

In the above example we defined factorised service di_connection, and below by accessing the service from di we created two separate connection to database.

Requesting services fromm dependency injection container

from kink import di
from sqlite3 import connect

# Setting services
di["db_name"] = "test_db.db"
di["db_connection"] = lambda di: connect(di["db_name"])


# Getting service

connection = di["db_connection"] # will return instance of sqlite3.Connection
assert connection == di["db_connection"] # True

Autowiring dependencies

from kink import di, inject
from sqlite3 import connect, Connection


di["db_name"] = "test_db.db"
di["db_connection"] = lambda di: connect(di["db_name"])

# Inject connection from di, connection is established once function is called.
@inject
def get_database(db_connection: Connection):
    ...


connection = get_database()
connection_with_passed_connection = get_database(connect("temp.db")) # will use passed connection

Constructor injection

from kink import inject, di
import MySQLdb

# Set dependencies
di["db_host"] = "localhost"
di["db_name"] = "test"
di["db_user"] = "user"
di["db_password"] = "password"
di["db_connection"] = lambda di: MySQLdb.connect(host=di["db_host"], user=di["db_user"], passwd=di["db_password"], db=di["db_name"])

@inject
class AbstractRepository:
    def __init__(self, db_connection):
        self.connection = db_connection


class UserRepository(AbstractRepository):
    ...


repository = UserRepository()
repository.connection # mysql db connection is resolved and available to use.

When class is annotated by inject annotation it will be automatically added to the container for future use (eg autowiring).

Services aliasing

When you registering service with @inject decorator you can attach your own alias name, please consider the following example:

from kink import inject
from typing import Protocol

class IUserRepository(Protocol):
    ...

@inject(alias=IUserRepository)
class UserRepository:
    ...


assert di[IUserRepository] == di[UserRepository] # returns true

For more examples check tests directory

Clearing di cache

from kink import inject, di

... # set and accesss your services

di.clear_cache() # this will clear cache of all services inside di container that are not factorised services

Project details


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.5.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

kink-0.5.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file kink-0.5.0.tar.gz.

File metadata

  • Download URL: kink-0.5.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.7.10 Linux/5.4.0-1040-azure

File hashes

Hashes for kink-0.5.0.tar.gz
Algorithm Hash digest
SHA256 71366d0255d0409f847e746f0a80e1f97f2b5d98b8e889fe0e97a8b47c31a56f
MD5 f2ed041d81d25e0c7d3ac90c2fc4f607
BLAKE2b-256 508b02b58918bb379cb00ed8b2309303ecfd45cc9bd54b9ec68ad80028aa1ae3

See more details on using hashes here.

File details

Details for the file kink-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: kink-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.7.10 Linux/5.4.0-1040-azure

File hashes

Hashes for kink-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93173c7250d8088430784ff65a911ce7fd9b51a4e531b86383c6b8801c4cc291
MD5 a4b6f0ef617cdf840056a578344e4b12
BLAKE2b-256 c8f4d4c815365adb6ef9395e9b813ed9cd2d27379a36064193c1904cf2cb1019

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page