simple dependency injection library
Project description
simple-di
A simple, strictly typed dependency injection library.
Install
pip install simple_di
Usage
Examples:
from simple_di import inject, Provide, Provider, container
from simple_di.providers import Static, Factory, Configuration
@container
class OptionsClass(container):
cpu: Provider[int] = Static(2)
worker: Provider[int] = Factory(lambda c: 2 * int(c) + 1, c=cpu)
Options = OptionsClass()
@inject
def func(worker: int = Provide[Options.worker]):
return worker
assert func() == 5
assert func(1) == 1
Options.worker.set(2)
assert func() == 2
Options.worker.reset()
assert func() == 5
Options.cpu.set(1)
assert func() == 3
API
Type annotation supported
inject
Inject values into providers in function/method arguments.
Arguments:
- squeeze_none: default False. Treat None value passed in as not passed.
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
simple_di-0.1.4.tar.gz
(8.9 kB
view hashes)
Built Distribution
Close
Hashes for simple_di-0.1.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69fe7d3b3d64ebc724f65b8cbb567b2559026f7873b6482bf48b2ae0b3af48b4 |
|
MD5 | cd6055aceaec6537d4828902e78c47ae |
|
BLAKE2b-256 | 9c56eee86a221badb0c9b5b1a3ee16e6fbbea4deb4c766e711e2c9350976054b |