Autowire

PyPI Package Version Documentation Status Codcov Coverage GitHub Stars

Autowire is light & simple dependency injection and resource management library for Python.

You can use dependency injection & resource management without any classes and any magics.

Since python already support nice context manager (PEP343), we don’t have to any extra interfaces for setting-up & tearing-down resource.

This is how to define resources in Autowire.

import contextlib
from autowire import Resource

# Define resources
db_connection_factory = Resource("db_connection_factory", __name__)
db_connection = Resource("db_connection", __name__)

# Implement db_connection resource
# db_connection is a resource to be implemented,
# db_connection_factory is a resource to be injected.
@db_connection.contextual(db_connection_factory)
@contextlib.contextmanager
def with_db_connection(db_connection_factory):
    conn = db_connection_factory()
    try:
        yield conn
    except:
        conn.rollback()
    finally:
        conn.close()

This is how to resolve resource implementations.

from autowire import Context

with Context() as context:
    conn = context.resolve(db_connection)
    conn.execute("SELECT * FROM ...")
    ...

Table of Contents

You can find more guides from following list:

Indices and tables