Skip to main content

Organize your config and environment variables

Project description

enver

Enver is a simple config / environment helper.

Test And Lint codecov GitHub release (latest by date including pre-releases) PyPI

Collect all your config values and environment variables in a single location, so new contributors knows what variables must be set, and don't have to go searching for os.environ or os.getenv scattered throughout the code.

Features:

  • Automatic validation of environment variables.
  • Automatic casting to desired data type using Pydantic.
  • Optional default values.
  • Fails fast if a variable has no value (either default or override).
  • Allows lookup using dot notation, e.g. env.DB_PASSWORD, as well as subscript (env['DB_PASSWORD']) or the get() method env.get('DB_PASSWORD').

Installation

pip install enver

Usage

Create a class that inherits from Enver, with config variables as class attributes. Putting this class in a separate config.py file might be a good idea, but is not necessary.

# config.py

from enver import Enver
from typing import Optional, List, Dict

class Config(Enver):
    MY_DB_HOST: str = "127.0.0.1"
    MY_DB_USER: str = "user"
    MY_DB_PASSWORD: str  # No default value, will be supplied as environment value.
    THIS_VAL_MIGHT_NOT_EXIST_IN_ENV: Optional[str]
    PI: float  # Read from env and converted to float, if possible.
    ENABLE_LOGGING: bool = True
    LOCATIONS: List[str] = ["/opt", "/etc"]
    MAPPING: Dict[str, float]

If environment values with the same name as any of the attributes exist, those will be used, overriding any defaults. Values will be converted to the specified type. If conversion is not possible, an exception is thrown.

Here's how to use the values:

# service.py

from .config import Config

env = Config()

user = env.MY_DB_USER
password = env.MY_DB_PASSWORD

if env.exists('FEATURE_1'):
  feature_1()

all_values = env.all()

The Enver class and any derived classes are singletons, meaning they always return the same instance:

env1 = Config()
env2 = Config()
assert env1 is env2

Which means this is fine:

user = Config().MY_DB_USER
password = Config().MY_DB_PASSWORD

Development

Dependencies for the project are managed with poetry. To install all dependencies run:

poetry install

To install pre-commit and set up its git hooks:

pip install pre-commit
pre-commit install

Now pre-commit will run automatically on every commit.

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

enver-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

enver-0.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file enver-0.2.0.tar.gz.

File metadata

  • Download URL: enver-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.2 Linux/5.8.0-1042-azure

File hashes

Hashes for enver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3a4a51e965ed165266a12cf3f567733b21f32eff67abde8810642fec90534683
MD5 4675b27beb6cda3771155006c305532b
BLAKE2b-256 36ef7395f1707c0f96a019f8726a490275df926f0745451d376cf64615e733c8

See more details on using hashes here.

File details

Details for the file enver-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: enver-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.2 Linux/5.8.0-1042-azure

File hashes

Hashes for enver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c02aa7865d43ffaea280642202fcdce08060a1f7ba434aadff677d8ace612e1
MD5 5cc888896abdae0fdfd4d233c3a56fab
BLAKE2b-256 367795f7feb02871478949b01f229a5c272d4db111fee4967fcc132af8ca2cb4

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