Skip to main content

Python dependency injection framework

Project description

Dependency Injector is a Python dependency injection framework. It was designed to be unified, developer-friendly tool for managing any kind of Python objects and their dependencies in formal, pretty way.

Dependency Injector framework key features are:

  • Easy, smart, pythonic style.
  • Obvious, clear structure.
  • Extensibility and flexibility.
  • Memory efficiency.
  • Thread safety.
  • Documentation.
  • Semantic versioning.

Status

PyPi Latest Version Downloads License
Python versions and implementations Supported Python versions Supported Python implementations
Builds and tests coverage Build Status Coverage Status

Installation

Dependency Injector library is available on PyPi:

pip install dependency_injector

Example

"""Dependency Injector example."""

import sys
import sqlite3

from boto.s3.connection import S3Connection

from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections

from example import services


class Platform(catalogs.DeclarativeCatalog):
    """Catalog of platform service providers."""

    database = providers.Singleton(sqlite3.connect, ':memory:')

    s3 = providers.Singleton(S3Connection,
                             aws_access_key_id='KEY',
                             aws_secret_access_key='SECRET')


class Services(catalogs.DeclarativeCatalog):
    """Catalog of business service providers."""

    users = providers.Factory(services.Users,
                              db=Platform.database)

    photos = providers.Factory(services.Photos,
                               db=Platform.database,
                               s3=Platform.s3)

    auth = providers.Factory(services.Auth,
                             db=Platform.database,
                             token_ttl=3600)


@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
    """Main function."""
    login, password, photo_path = argv[1:]

    user = users_service.get_user(login)
    auth_service.authenticate(user, password)
    photos_service.upload_photo(user['id'], photo_path)


if __name__ == '__main__':
    main(sys.argv)

You can get more Dependency Injector examples in /examples directory on GitHub:

https://github.com/ets-labs/dependency_injector

Documentation

Dependency Injector documentation is hosted on ReadTheDocs:

Feedback

Feel free to post questions, bugs, feature requests, proposals etc. on Dependency Injector GitHub Issues:

https://github.com/ets-labs/dependency_injector/issues

Your feedback is quite important!

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
dependency_injector-1.16.4.tar.gz (28.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page