A simple and functional dependency injection toolkit for Python
A simple and functional dependency injection toolkit for Python.
PyDio aims to be simple, yet still powerful, allowing you to feed dependencies inside your application in a flexible way. PyDio design is based on simple assumption, that dependency injection can be achieved using simple key-to-function map, where key specifies type of object you want to inject and function is a factory function that creates instances of that type.
In PyDio, this is implemented using providers and injectors. You use providers to configure your key-to-function mapping, and then you use injectors to perform a lookup of a specific key and creation of the final object.
Here’s a simple example:
import abc from pydio.api import Provider, Injector provider = Provider() @provider.provides('greet') def make_greet(): return 'Hello, world!' def main(): injector = Injector(provider) greet_message = injector.inject('greet') print(greet_message)
And if you now call main() function, then the output will be following:
- Support for any hashable keys: class objects, strings, ints etc.
- Support for any type of object factories: function, coroutine, generator, asynchronous generator.
- Automatic resource management via generator-based factories (similar to pytest’s fixtures)
- Multiple environment support: testing, development, production etc.
- Limiting created object’s lifetime to user-defined scopes: global, application, use-case etc.
- No singletons used, so there is no global state…
- …but you still can create global injector on your own if you need it :-)
You can install PyDio using one of following methods:
From PyPI (for stable releases):
$ pip install PyDio
From test PyPI (for stable and development releases):
$ pip install -i https://test.pypi.org/simple/ PyDio
Directly from source code repository (for all releases):
$ pip install git+https://gitlab.com/zef1r/PyDio.git@[branch-or-tag]
You have two options available:
This project is released under the terms of the MIT license.
See LICENSE.txt for more details.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size PyDio-0.1.0-py3-none-any.whl (14.6 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size PyDio-0.1.0.tar.gz (29.6 kB)||File type Source||Python version None||Upload date||Hashes View|