Skip to main content

Inject dependencies via environment variables

Project description

depenv

Depenv varies Python classes at runtime based on environment variables. For example:

import depenv

class Bird(depenv.Injectable):
  def speak(self):
    return "chirp"

class Duck(Bird):
  def speak(self):
    return "quack"

Without any environment variables:

>>> Bird().speak()
"chirp"
>>> isinstance(Bird(), Bird)
True
>>> isinstance(Bird(), Duck)
False

With DEPENV_BIRD=Duck:

>>> Bird().speak()
"quack"
>>> isinstance(Bird(), Bird)
True
>>> isinstance(Bird(), Duck)
True

Depenv aims to provide the benefits of dependency injection without the boilerplate of other dependency injection frameworks.

Installation

Depenv is available on PyPi:

pip install depenv

Depenv has no external dependencies and works for all supported Python versions.

Usage

When you create a new object using a class that subclasses depenv.Injectable, Depenv will search your environment variables for a suitable class to inject. For example:

import depenv

class Bird(depenv.Injectable):
  def speak(self):
    return "chirp"

class Duck(Bird):
  def speak(self):
    return "quack"

All Depenv environment variables are prefixed with DEPENV_.

If you do not set an environment variable, Depenv will use the injectable class:

>>> Bird().speak()
"chirp"

Configuring the injected class

The simplest Depenv configuration specifies only the injectable class name and the injected class name. For example, with DEPENV_BIRD=Duck:

>>> Bird().speak()
"quack"

If your injected class exists in a different module, you can provide an absolute path. For example, with DEPENV_BIRD=examples.birds.Duck:

>>> Bird().speak()
"quack"

Configuring the injectable class

If your injectable class shares a name with another injectable class, you can disambiguate by including a module. For example:

  • DEPENV_BIRD will match any injectable class named Bird (case insensitive) in any module.
  • DEPENV_Bird will match any injectable class named Bird (case sensitive) in any module.
  • DEPENV_EXAMPLES_BIRDS_BIRD will match any injectable class named Bird (case insensitive) in the examples.birds (case insensitive) module.
  • DEPENV_examples_birds_Bird will match the injectable class named Bird (case sensitive) in the examples.birds (case sensitive) module.

Because . is in invalid charcter in environment variable names, Depenv uses _ in its place. Consequently, all of the following will matchthe environment variable DEPENV_examples_birds_Bird:

  • examples.birds.Bird
  • examples.birds_Bird
  • examples_birds.Bird
  • examples_birds_Bird

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

depenv-1.0.1.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

depenv-1.0.1-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

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