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.0.tar.gz
.
File metadata
- Download URL: typedi-0.4.0.tar.gz
- Upload date:
- Size: 4.3 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 | 9a8d16cc4415caeb6d7df086822b2631be3003776c4edaf787b7e4b4d19abfb4 |
|
MD5 | d2cd702a6e13ec25534b5c341c486ed0 |
|
BLAKE2b-256 | ec745b3c40cfa017378c7a66c311c9ad74fbac28fcffaba554d513ed11097800 |
File details
Details for the file typedi-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: typedi-0.4.0-py3-none-any.whl
- Upload date:
- Size: 5.1 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 | e2dab84a27c9116f3aeb30445660ffcae17c3a7ae58bdfc4dd4a3b9c0a618c8e |
|
MD5 | a573a0f30122685bdea1863aaac6c119 |
|
BLAKE2b-256 | c6820e5e706500b22109866762c0ec140ed58bed69672620958d703a5d8042cb |