simple dependency injection library
Project description
simple-di
A simple dependency injection library
Install
pip install simple_di
Usage
Examples:
from simple_di import inject, Provide, Provider
from simple_di.providers import Static, Callable, Configuration
class Options(Container):
cpu: Provider[int] = Static(2)
worker: Provider[int] = Callable(lambda c: 2 * c + 1, c=cpu)
@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.
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.0.2.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for simple_di-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8234fdda4a24db3b6683ce50d895e48baaa0a7b17be94717db1794fc03681cb4 |
|
MD5 | 018d812e7ec3b21d666a7cda135f489e |
|
BLAKE2b-256 | ff13c9ad6afae6379f25574d785881350cd67e20b9d72bd1ff84352b8cbad094 |