Skip to main content

An environment variable parser library with type hint and conversion support.

Project description

Python

License

Mypy Ruff uv

enve

An environment variable parser library with type hint and conversion support.

The complete documentation is available here.

Usage

enve.get is the main package function. It's essentially os.getenv on (arguably unnecessarily strong) steroids. It's advantage is that it will automatically validate the environment variable's data type based on what you desire and provide typing support for the used variable. For complete details, see the enve.get function docstring.

The example below illustrates how you can obtain environment variables and convert them to a desired data type.

import enve


ENVVAR = enve.get("ENVVAR")  # will be parsed and typed as `str`
ENVVAR_STR = enve.get("ENVVAR", dtype=str)  # will be parsed and typed as `str`
MY_SWITCH = enve.get("MY_SWITCH", dtype=bool)  # will be parsed and typed as `bool`
HASH_SALT = enve.get("HASH_SALT", dtype=bytes)  # will be parsed and typed as `bytes`
RANDOM_SEED = enve.get("RANDOM_SEED", dtype=int)  # will be parsed and typed as `int`
MY_PI = enve.get("MY_PI", dtype=float)  # will be parsed and typed as `float`
CUDA_VISIBLE_DEVICES = enve.get("CUDA_VISIBLE_DEVICES", dtype=list)  # will be parsed and typed as `list`

Supported data types are bool, bytes, float, int, str (the default), and list.

In all these examples, a ValueError will be raised if the environment variable is not defined. To use a default value, set the default parameter accordingly. Note that the default value's type must be None or the same as the expected return type.

import enve


ENVVAR_WITH_DEFAULT = enve.get("MISSING_ENVVAR", default="foobar")

enve.get also supports Docker secrets. By setting the docker_secret parameter to True or to the name of a Docker secret, enve.get will attempt to retrieve its value (even if unset in the current environment).

import enve


# The following snippet will:
# 1. Check whether the `MY_SECRET` envvar exists. If true, this will be returned.
# 2. Check whether `/run/secrets/MY_SECRET` exists. If true, the file will be read
#    and the value will be returned.
# 3. Check whether `/run/secrets/my_secret` exists. If true, the file will be read
#    and the value will be returned.
# 4. Raise a `ValueError` as no default is provided.
MY_SECRET = enve.get("MY_SECRET", docker_secret=True)

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

enve-0.1.0.tar.gz (79.0 kB view details)

Uploaded Source

File details

Details for the file enve-0.1.0.tar.gz.

File metadata

  • Download URL: enve-0.1.0.tar.gz
  • Upload date:
  • Size: 79.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for enve-0.1.0.tar.gz
Algorithm Hash digest
SHA256 758abeeccaaf21b216c04a0e3f5da4dbd31b78bb8bd63f55c76537b49367cf19
MD5 9dce46dd37e5ff8e704848b514209d9b
BLAKE2b-256 f8b14aaea1559fb1387d8d13b7f6cac4b6e83b0e607b75ca523e6bd58fd857b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for enve-0.1.0.tar.gz:

Publisher: release.yaml on aachick/enve

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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