Skip to main content

Async-first dependency injection library for Python

Project description

aiodine

python pypi travis black codecov license

aiodine provides async-first dependency injection in the style of Pytest fixtures for Python 3.6+.

Installation

pip install aiodine

Usage

Note: this section is under construction.

aiodine revolves around two concepts: providers and consumers.

Providers are:

  • Explicit: once a provider is defined, a consumer can use it by declaring it as a function parameter.
  • Modular: a provider can itself use other provider.
  • Flexible: providers are reusable within the scope of a function or a whole session, and support a variety of syntaxes (asynchronous or synchronous, function or generator) to make provisioning resources fun again.

Providers

Providers make a resource available to consumers within a certain scope. They are created by decorating a provider function with @aiodine.provider.

Here's a "hello world" provider:

import aiodine

@aiodine.provider
async def hello():
    return "Hello, aiodine!"

Tip: synchronous provider functions are also supported!

Providers are available in two scopes:

  • function: the provider's value is re-computed everytime it is consumed.
  • session: the provider's value is computed only once (the first time it is consumed) and is reused in subsequent calls.

By default, providers are function-scoped.

Consumers

Once a provider has been declared, it can be used by consumers. A consumer is built by decoratinga consumer function with @aiodine.consumer. A consumer can declare a provider as one of its parameters and aiodine will inject it at runtime.

Here's an example consumer:

@aiodine.consumer
async def show_friendly_message(hello):
    print(hello)

Tip: synchronous consumer functions are also supported!

All aiodine consumers are asynchronous, so you'll need to run them in an asynchronous context:

from asyncio import run

async def main():
    await show_friendly_message()

run(main())  # "Hello, aiodine!"

A consumer can also define any extra non-provider parameters. These must be declared after provider parameters in order for aiodine to correctly inject the provided values to the correct parameters. When calling the consumer, extra arguments can be passed as usual.

@aiodine.consumer
async def show_friendly_message(hello, repeat=1):
    for _ in range(repeat):
        print(hello)

async def main():
    await show_friendly_message(repeat=10)

Providers consuming other providers

Providers can also consume other providers. To do so, providers need to be frozen so that the dependency graph can be correctly resolved:

import aiodine

@aiodine.provider
async def email():
    return "user@example.net"

@aiodine.provider
async def send_email(email):
    print(f"Sending email to {email}…")

aiodine.freeze()  # <- Ensures that `send_email` has resolved `email`.

A context manager is also available:

import aiodine

with aiodine.exit_freeze():
    @aiodine.provider
    async def email():
        return "user@example.net"

    @aiodine.provider
    async def send_email(email):
        print(f"Sending email to {email}…")

Note: thanks to this, providers can be declared in any order.

Generator providers

Generator providers can be used to perform cleanup operations after a provider has gone out of scope.

import os
import aiodine

@aiodine.provider(scope="session")
async def testing():
    initial = os.getenv("APP_ENV")
    os.environ["APP_ENV"] = "TESTING"
    try:
        yield
    finally:
        os.environ.pop("APP_ENV")
        if initial is not None:
            os.environ["APP_ENV"] = initial

Tip: synchronous generator providers are also supported!

Lazy async providers

When the provider function is asynchronous, its return value is awaited before being injected into the consumer. In other words, providers are eager by default.

You can mark a provider as lazy in order to defer awaiting the provided value to the consumer. This is useful when the provider needs to be conditionally evaluated.

from asyncio import sleep
import aiodine

@aiodine.provider(lazy=True)
async def expensive_computation():
    await sleep(10)
    return 42

@aiodine.consumer
async def compute(expensive_computation, cache=None):
    if cache:
        return cache
    return await expensive_computation

FAQ

Why "aiodine"?

aiodine contains aio as in asyncio, and di as in Dependency Injection The last two letters are only there to make this library's name pronounce like iodine, the chemical element.

Changelog

See CHANGELOG.md.

License

MIT

Project details


Download files

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

Source Distribution

aiodine-0.1.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

aiodine-0.1.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file aiodine-0.1.1.tar.gz.

File metadata

  • Download URL: aiodine-0.1.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for aiodine-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9b896effa60363c59bcb4ffb6e29ce69a5af279e6216fca7713c7fa2124bd33c
MD5 c234a0bc4e1b695e939e13613111b0fc
BLAKE2b-256 11f78a3d22ea3ba4643685c58d1f6bfa534ee55aca45c48dbfbac6b2f59b2981

See more details on using hashes here.

File details

Details for the file aiodine-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: aiodine-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for aiodine-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2ab6840fe00666b797a1398ff5d17276d857ed77487df86daee1d8002fbcb0a
MD5 9f75be698b12120cf783d682d9eef8fd
BLAKE2b-256 bbaad547738f401eccd480fcbf6febfa868881a550a3f6d8625aaace8a026b77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page