Skip to main content

A simple module for environment variable based configuration

Project description

Typed getenv

A simple package for minimalistic environment-based configurations. No dependencies required.


What this module is for

As microservices become more and more popular there's a need for environment based configuration.

Why not use a config ini/YAML/JSON/etc. file? Microservices are mostly run in containers. Sometimes you might not have access to the host environment to build containers yourself with filled config files embedded into them or mount files into the container. The most logical option then is to provide a prebuilt container image and a set of environment variables that define it's behaviour.

Why not use standard os.getev()? Getenv is mostly for storing strings, however configurations often need to include integers, floats and logical values. Getenv would require some very trivial custom logic to be rewritten over and over again to convert strings into the desired types of values and validate them. This library provides this code.

Why create a new package? Indeed there are already solutions to the problems listed above. environ-config and Pydantic are nice tools that would be great for that job, but they're clearly an overkill for the task when you have a lightweight microservice application with only a few environment variables to parse. This package on the other hand has no dependencies and provides global access to the needed variables across all your application no initialization and configuration required.

Installation

To acquire the module head over to the terminal and install the module using your favourite package manager e.g. pip instal typed_getenv or poetry add typed_getenv.

Usage

Typed getenv has a very simple interface that is pretty much just a wrapper over standard os.getenv() with a couple additional arguments and some custom exceptions.

This example demonstrates the usage of the module.

from typing import Optional

# Import the getenv function from the package
from typed_getenv import getenv

# Get an optional string parameter
# Variable name and the default value are positional (as in os.getenv()) but "var_type" and "optional"
# arguments are strictly keyword. "var_type" defaults to Optional[str] and "optional" defaults to False.
TEST_OPTIONAL_STR_VALUE: Optional[str] = getenv("TEST_OPTIONAL_STR_VALUE", optional=True)

# Get a mandatory string parameter (if unset raises VariableUnsetError)
# Note that although the default is set it will still raise an exception unless the "optional" argument is set to True
TEST_STR_VALUE: str = getenv("TEST_STR_VALUE", default="foo", var_type=str)

# Get an integer (can also be optional).
# If type conversion is not possible - raises TypeMismatchError
TEST_INT_VALUE: int = getenv("TEST_INT_VALUE", 42, var_type=int)

# Get a float (can also be optional)
TEST_FLOAT_VALUE: float = getenv("TEST_FLOAT_VALUE", 4.2, var_type=float)

# Get a bool value
# Strings "1", "yes", "true", "on" and "enable" will be interpreted as True
# Strings "0", "no", "false", "off" and "disable" will be interpreted as False
# Case doesn't matter. Other values will result in raise of UnprocessableValueError
TEST_BOOL_VALUE: bool = getenv("TEST_BOOL_VALUE", False, var_type=bool)

Contributing

If you want to contribute to the development - file an issue or create a pull request on the GitHub page for this module.

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

typed-getenv-0.1.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

typed_getenv-0.1.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file typed-getenv-0.1.2.tar.gz.

File metadata

  • Download URL: typed-getenv-0.1.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1022-azure

File hashes

Hashes for typed-getenv-0.1.2.tar.gz
Algorithm Hash digest
SHA256 515c1c946701cfc24205ff7d95b485cf1e85fc283b5049746a2cc9c4e3eae0d0
MD5 d4e06b05c7a27ffbe6efc9318d550a9b
BLAKE2b-256 024e80f5e26a634e40c077cd69353892b3234c0ccab13dcb035735460f62dbfd

See more details on using hashes here.

File details

Details for the file typed_getenv-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: typed_getenv-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1022-azure

File hashes

Hashes for typed_getenv-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c45bcfab04157a29bdc89969ee33939f162688ff01b2092f46c5d2f06eb6e3e
MD5 715d023f47fa0e0281be9c5d6916d5ba
BLAKE2b-256 d7d13f1e4a0899aaf5a64bb3c7c4b1c8919a96d7b416cb4dc78c93b7f0650dbd

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