Python Dependency Injection Framework

2019-01-21 02:03发布

Is there a framework equivalent to Guice (http://code.google.com/p/google-guice) for Python?

16条回答
手持菜刀,她持情操
2楼-- · 2019-01-21 02:15

If you want a guice like (the new new like they say), I recently made something close in Python 3 that best suited my simple needs for a side project.

All you need is an @inject on a method (__init__ included of course). The rest is done through annotations.

from py3njection import inject
from some_package import ClassToInject

class Demo:
    @inject
    def __init__(self, object_to_use: ClassToInject):
        self.dependency = object_to_use

demo = Demo()

https://pypi.python.org/pypi/py3njection

查看更多
老娘就宠你
3楼-- · 2019-01-21 02:15

I recently released a neat (IMHO) micro library for DI in python:

https://github.com/suned/serum

查看更多
一夜七次
4楼-- · 2019-01-21 02:19

As an alternative to monkeypatching, I like DI. A nascent project such as http://code.google.com/p/snake-guice/ may fit the bill.

Or see the blog post Dependency Injection in Python by Dennis Kempin (Aug '08).

查看更多
干净又极端
5楼-- · 2019-01-21 02:19

I made a lib to do this https://github.com/ettoreleandrotognoli/python-cdi I hope that helps

It's available on pypi: https://pypi.python.org/pypi/pycdi

With it you can make injections with python2

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)

And using type hints from python3

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)
查看更多
beautiful°
6楼-- · 2019-01-21 02:20

If you just want to do dependency injection in Python, you don't need a framework. Have a look at Dependency Injection the Python Way. It's really quick and easy, and only c. 50 lines of code.

查看更多
何必那么认真
7楼-- · 2019-01-21 02:21

I like this simple and neat framework.

http://pypi.python.org/pypi/injector/

Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.

That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalized code through the use of Module s.

While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.

查看更多
登录 后发表回答