Skip to main content

Load environment variables Enum style

Project description

Environment variables

pytest pypi license

Enum style access to environment variables with type annotations

~ Av vars och env efter förmåga, åt vars och env efter behov ~

The package is hosted at PyPI

Documentation

The documentation can be found on ReadTheDocs

Requirements

This package supports Python 3.7 or later

Installation

Install using pip:

$ pip install environment-variables

Usage

Define your environment variables as class attributes with type annotation:

from environment_variables import environment_variables


@environment_variables
class Environment:
    MY_VARIABLE: str
    MY_INTEGER: int = 10
    MY_FEATURE_FLAG: bool = False

When accessing a class attribute, the class will automatically check the system for a environment variable of the same name and return its value cast to the annotated type. If it is not defined, the default value will be used instead.

It is also possible to annotate a class attribute with any class using the variables function:

from environment_variables import environment_variables, variable


@environment_variables
class Environment:
    MY_VARIABLE: CustomClass = variable(
        CustomClass,
        default='some default value',
        default_factory=custom_class_factory,
        args=(1, 2, 3,),
        kwargs={'more_custom': True},
    )

The problem this is trying to solve

When configuring a python program with environment variables, one would typically access them in a fashion similar to this:

import os

my_value = os.getenv('MY_VALUE', default=123)

This leaves a lot of strings lying around in the code, and it gets hard to keep track on which values are being used and what variables are needed to be set when. A better approach would be to collect everything in a config file:

import os

class MyConfig:
    @classmethod
    def get_my_value(cls, default):
        return os.getenv('MY_VALUE', default=default)

This makes it slightly easier to keep track of, but we are still using strings that we have to keep track of. An even better approach would be to use Enums:

import os
import enum

class MyVariables(enum.Enum):
    MY_VALUE = 'MY_VALUE'

class MyConfig:
    @classmethod
    def get_my_value(cls, default):
        return os.getenv(MyVariables.MY_VALUE.value, default=default)

Much better, now we can just look at the enum to see what variables we have, but there is a lot of boilerplate code. For instance, do we really have to write out 'MY_VALUE' twice in the enum definition? It would be much more convenient to have the 'MyVaribles' class understand that the attribute name should be the environment variable to look for, instead of having to specify the string name of the variable again.

On top of that, os.getenv always returns strings, so we would have to take care of the type casting ourselves if we want to have server ports as integers or feature flags as booleans.

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

environment-variables-0.1.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

environment_variables-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file environment-variables-0.1.0.tar.gz.

File metadata

  • Download URL: environment-variables-0.1.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for environment-variables-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc43ab330e5a1da8a41246576fd9f5fe4de9bf593a78dcbcb99571c38c5a11f2
MD5 032efe437596e155a67a8f5c0e01bc43
BLAKE2b-256 de00f7abe74e54224e752bef9fe317df12603a8621c8d9c183db26fe9ad09368

See more details on using hashes here.

File details

Details for the file environment_variables-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: environment_variables-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for environment_variables-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 565662c80262184f70daf3e34fd1e2a642c922d2ea47a057f0052841e179d906
MD5 4e0d191de7ce4fa9128243d76c1a9bdb
BLAKE2b-256 517c4d774eb21c6967cd8cdca34a301d4318143bcf94e144411b54aa5998c135

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