Simple yet powerful typed dependency injection container
Project description
typedi
Simple yet powerful typed dependency injection container.
To install from python package index simply type (no dependencies):
pip install typedi
Or if you don't want to bring a dependency inside your project simply copy and paste typedi.py
file (and dont forget the tests).
Usage
Common usage scenario
config.py
from dataclasses import dataclass
@dataclass
class DatabaseConfig:
host: str
username: str
password: str
@dataclass
class AppConfig:
debug: bool = False
app.py
from config import DatabaseConfig, AppConfig
class Application:
def __init__(self, app_conf: AppConfig, db_config: DatabaseConfig):
pass
def run(self):
pass
main.py
from typedi import container
from config import DatabaseConfig, AppConfig
from app import Application
def load_db_config_from_file() -> DatabaseConfig:
# Load config from file... and intantiate a config object
return DatabaseConfig(host='localhost', username='user', password='pass')
if __name__ == '__main__':
container.register_singleton_factory(load_db_config_from_file)
container.register_singleton_class(AppConfig)
container.register_class(Application)
# When accessing the instance typedi will automatically resolve all required dependencies
# provided in __init__ annotations
application_with_initialized_configs = container.get_instance(Application)
application_with_initialized_configs.run()
Containers
typedi comes with a default shared container, to add or retrieve instances from it import it anywhere you need - usually in some initialization/bootstrapping logic.
from typedi import container
Or you could also create your own DI containers:
from typedi import Container
my_container = Container()
Instance bindings, "user-managed singletons"
from typedi import container
class MyClass:
pass
instance = MyClass()
container.register_instance(instance)
# anywhere else
from typedi import container
instance = container.get_instance(MyClass)
Class bindings
from typedi import container
class MyClass:
pass
container.register_class(MyClass)
# anywhere else
from typedi import container
auto_instantiated_instance = container.get_instance(MyClass)
Class bindings with inheritance
from typedi import container
class MyClass:
pass
class ChildOfMyClass(MyClass):
pass
container.register_class(ChildOfMyClass)
# anywhere else
from typedi import container
auto_instantiated_instance = container.get_instance(MyClass) # type: ChildOfMyClass
Testing
We are using tox (and pytest) to test among multiple python versions. To run test suites and generate coverage reports simply execute
tox
If you don't have tox installed, execute pip install tox
first.
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
Built Distribution
File details
Details for the file typedi-0.4.1.tar.gz
.
File metadata
- Download URL: typedi-0.4.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cddfb27e7e8e31ad2525698486805bf900d3744360d8525466eda93330200ac |
|
MD5 | 016abc46765b047441fc26cceb5dc9f4 |
|
BLAKE2b-256 | 7d00a18a6601947a0a3e6bcd38fc4a2f82847c33a6ced0c5542bd40d43f4529d |
File details
Details for the file typedi-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: typedi-0.4.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 420c09781ed3c388a43525e876525e07a50d30b27f646c9dbd8f8523884a5903 |
|
MD5 | 09435459ab5157dea32c3e9e51e6a9dd |
|
BLAKE2b-256 | c2ebfa0505cd676c77de1ce5ac00133d97c02b6a3a2b2d74cb7ad7163df5cfa7 |