PyCDI
A simple Python CDI ( Code Dependency Injection) Library.
See the code of conduct.
Install
Install stable pycdi
pip install pycdi
Install latest pycdi
pip install git+https://github.com/ettoreleandrotognoli/python-cdi
Usage
Python2 & Python3
You can see more examples in the examples folder (examples/common).
import logging
from logging import Logger
from pycdi import Inject, Singleton, Producer
from pycdi.shortcuts import call
@Producer(str, _context='app_name')
def get_app_name():
return 'PyCDI'
@Singleton(produce_type=Logger)
@Inject(app_name=str, _context='app_name')
def get_logger(app_name):
return logging.getLogger(app_name)
@Inject(name=(str, 'app_name'), logger=Logger)
def main(name, logger):
logger.info('I\'m starting...')
print('Hello World!!!\nI\'m a example of %s' % name)
logger.debug('I\'m finishing...')
call(main)
Python 3
With Python 3 is possible define the types of injections with the type hints.
You can see more examples in the examples folder( examples/py3/ ).
import logging
from logging import Logger
from pycdi import Inject, Singleton, Producer
from pycdi.shortcuts import call
@Producer(_context='app_name')
def get_app_name() -> str:
return 'PyCDI'
@Singleton()
@Inject(logger_name='app_name')
def get_logger(logger_name: str) -> Logger:
return logging.getLogger(logger_name)
@Inject(name='app_name')
def main(name: str, logger: Logger):
logger.info('I\'m starting...')
print('Hello World!!!\nI\'m a example of %s' % name)
logger.debug('I\'m finishing...')
call(main)
Release files for pycdi 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pycdi-0.1.2.tar.gz | 10.3 kB | Details |
Release files / pycdi-0.1.2.tar.gz
| Download URL | pycdi-0.1.2.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7213d607ae1e8a76d252ef3c86a99eca7f5e3c32c58fd8a3bc0bffbccde41f06
|
|
BLAKE2b-256 checksum How to use checksums |
cb33f4570940290e273790f70c47b4918a73a496842a633b9e584241678e6b4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8
|