Skip to main content

Dependency injection microframework for Python

Project description

https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/logo.svg

Latest Version License Supported Python versions Supported Python implementations Downloads Downloads Downloads Wheel Build Status Docs Status Coverage Status

What is Dependency Injector?

Dependency Injector is a dependency injection framework for Python.

Why do I need it?

Dependency Injector helps you understand and change the structure of the application.

With the Dependency Injector you keep application structure in one place. This place is called the container. You use the container to manage all the components of the application. All the component dependencies are defined explicitly. This provides the control on the application structure. It is easy to understand and change it.

https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/di-map.svg

The container is like a map of your application. You always know what depends on what.

Flask + Dependency Injector example application container:

from dependency_injector import containers, providers
from dependency_injector.ext import flask
from flask import Flask
from flask_bootstrap import Bootstrap
from github import Github

from . import views, services


class ApplicationContainer(containers.DeclarativeContainer):
    """Application container."""

    app = flask.Application(Flask, __name__)

    bootstrap = flask.Extension(Bootstrap)

    config = providers.Configuration()

    github_client = providers.Factory(
        Github,
        login_or_token=config.github.auth_token,
        timeout=config.github.request_timeout,
    )

    search_service = providers.Factory(
        services.SearchService,
        github_client=github_client,
    )

    index_view = flask.View(
        views.index,
        search_service=search_service,
        default_query=config.search.default_query,
        default_limit=config.search.default_limit,
    )

Running such container looks like this:

from .containers import ApplicationContainer


def create_app():
    """Create and return Flask application."""
    container = ApplicationContainer()
    container.config.from_yaml('config.yml')
    container.config.github.auth_token.from_env('GITHUB_TOKEN')

    app = container.app()
    app.container = container

    bootstrap = container.bootstrap()
    bootstrap.init_app(app)

    app.add_url_rule('/', view_func=container.index_view.as_view())

    return app

And testing looks like:

from unittest import mock

import pytest
from github import Github
from flask import url_for

from .application import create_app


@pytest.fixture
def app():
    return create_app()


def test_index(client, app):
    github_client_mock = mock.Mock(spec=Github)
    # Configure mock

    with app.container.github_client.override(github_client_mock):
        response = client.get(url_for('index'))

    assert response.status_code == 200
    # Do more asserts

See complete example here - Flask + Dependency Injector Example

How to install?

  • The package is available on the PyPi:

    pip install dependency-injector
    

Where is the docs?

Have a question?

Found a bug?

Want to help?

  • ⭐️ Star the Dependency Injector on the Github
  • 🆕 Start a new project with the Dependency Injector
  • 💬 Tell your friend about the Dependency Injector

Want to contribute?

  • 🔀 Fork the project
  • ⬅️ Open a pull request to the develop branch

Release history Release notifications | RSS feed

Download files

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

Files for dependency-injector, version 3.26.0
Filename, size File type Python version Upload date Hashes
Filename, size dependency_injector-3.26.0-cp27-cp27m-macosx_10_9_x86_64.whl (356.9 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27m-manylinux1_i686.whl (1.3 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27m-manylinux1_x86_64.whl (1.4 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27m-manylinux2010_i686.whl (1.3 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27m-manylinux2010_x86_64.whl (1.4 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27mu-manylinux1_i686.whl (1.3 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27mu-manylinux1_x86_64.whl (1.4 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27mu-manylinux2010_i686.whl (1.3 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp27-cp27mu-manylinux2010_x86_64.whl (1.4 MB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-macosx_10_9_x86_64.whl (366.7 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-manylinux1_i686.whl (1.6 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-manylinux1_x86_64.whl (1.7 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-manylinux2010_i686.whl (1.6 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-manylinux2010_x86_64.whl (1.7 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-win32.whl (183.9 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp35-cp35m-win_amd64.whl (226.4 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-macosx_10_9_x86_64.whl (400.9 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-manylinux1_i686.whl (1.6 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-manylinux1_x86_64.whl (1.8 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-manylinux2010_i686.whl (1.6 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-manylinux2010_x86_64.whl (1.8 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-win32.whl (197.4 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp36-cp36m-win_amd64.whl (240.7 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-macosx_10_9_x86_64.whl (368.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-manylinux1_i686.whl (1.6 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-manylinux1_x86_64.whl (1.7 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-manylinux2010_i686.whl (1.6 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-manylinux2010_x86_64.whl (1.7 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-win32.whl (197.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp37-cp37m-win_amd64.whl (241.6 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-macosx_10_9_x86_64.whl (382.0 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-manylinux1_i686.whl (2.1 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-manylinux1_x86_64.whl (2.2 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-manylinux2010_i686.whl (2.1 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-manylinux2010_x86_64.whl (2.2 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-win32.whl (206.2 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-cp38-cp38-win_amd64.whl (258.5 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp27-pypy_73-macosx_10_9_x86_64.whl (267.5 kB) File type Wheel Python version pp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp27-pypy_73-manylinux1_x86_64.whl (293.8 kB) File type Wheel Python version pp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp27-pypy_73-manylinux2010_x86_64.whl (293.8 kB) File type Wheel Python version pp27 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp36-pypy36_pp73-macosx_10_9_x86_64.whl (265.6 kB) File type Wheel Python version pp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp36-pypy36_pp73-manylinux1_x86_64.whl (294.6 kB) File type Wheel Python version pp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl (294.6 kB) File type Wheel Python version pp36 Upload date Hashes View
Filename, size dependency_injector-3.26.0-pp36-pypy36_pp73-win32.whl (189.5 kB) File type Wheel Python version pp36 Upload date Hashes View
Filename, size dependency-injector-3.26.0.tar.gz (410.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page