Python Code Dependency Injection Library
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples doc & example Feb 14, 2017
pycdi list to tuple Jun 19, 2018
tests boot ok May 3, 2018
.gitignore boot ok May 3, 2018
.travis.yml codeclimate coverage Jun 19, 2018
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md Jun 19, 2017
LICENSE Create LICENSE Jun 19, 2017
Makefile producer correction & test Jun 19, 2017
README.rst badges Jun 19, 2018
codecov.yml
setup.py bump version Apr 25, 2018

README.rst

PyCDI

https://travis-ci.org/ettoreleandrotognoli/python-cdi.svg?branch=master Maintainability Test Coverage CodeFactor

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)