DIPPY: Dependency Injection for Python

DIPPY is a simple dependency injection container for Python. It supports both constructor and 'setter' (actually direct attribute access) injection.

Furthermore I make a distinction between the following service types:

  1. Factory services - which exist to create multiple instances of objects. A factory service will fulfil dependencies of created objects, but allow other constructor arguments to be specified at creation time. The container provides an appropriately configured factory, and container clients actually instantiate the object themselves.
  2. Singleton services - which are managed by the container. Only a single instance will ever be created. Additional constructor arguments may be passed at registration time.
  3. Instance services - useful for things such as configuration settings, flags, strings etc. The container is provided with the object instance, rather than the class.

Example:

  class Article:
    constructor_dependencies = dict(db='database')
  
    def __init__(db, id=None):
      self.db = db

      if id:
        self.load(id)
  
  container.register_service('db_password', 'very_s3cret')
  container.register_service('db_host', '127.0.0.1')
  container.register_service('db_user', 'mydbuser')
  container.register_singleton_service('database', MysqlDatabase)
  container.register_factory_service('article', Article)
  a = container.article(id=234)  # instantiate article id 234

Download: dippy.tgz

Acknowledgements: