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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file depenv-1.0.1.tar.gz.

File metadata

  • Download URL: depenv-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for depenv-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cb552de9574acee988b616fcff7c9f62ec6497972771183ae1b433811f2b2d0f
MD5 2d36d1c649213d6945030041cf8d8eda
BLAKE2b-256 7bd7401b155f4b2a67a15acc3f133e9b3127a748fd9996fece54f881beeee567

See more details on using hashes here.

File details

Details for the file depenv-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: depenv-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for depenv-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4683173f0df5400c006f07e1736cc5ae72c179adfbc9dc117629120d271ba11
MD5 719a960005318dd1df4eda655c6e6d27
BLAKE2b-256 d86ad9973b0cea2df7143b1457b9017e15924e4b2f7a6782e13ff31943f332a0

See more details on using hashes here.

Supported by

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