Skip to main content

Simple way to parse common datatypes from env vars.

Project description

magicenv

Simple way to parse common datatypes from env vars.

Setup

pip install magicenv

Usage

from magicenv import env

DB_HOST = env('DB_HOST', "localhost:1234")              # string var
DB_NUM_TRANSACTIONS = env('DB_NUM_TRANSACTIONS', 1234)  # int var
ENABLE_FEATURE_X = env('ENABLE_FEATURE_X', True)  # bool var

DB_SERVERS = env('DB_SERVERS', ['server1', "server2"])  # interprets a comma separated string as a list

env()

def env(key, default=None, return_type=None, list_element_type=str):
Param Explanation
key name of environment variable.
default Optional - default value if key is not present in environment
return_type Optional - specify return_type.
list_element_type Optional - cast/parse each element of a list with provided callable. Only used if the setting is a list.

​ If key is not present in environment, and there is not provided a default value, ​ None will be returned.

Guidelines for settings in Environment

A couple of conventions exist when designing environment variables for settings.

  1. All values are stored as strings in the environment variable

  2. Booleans All boolean variables are encoded as one of "1", "True" or "true" if True, all other values are interpreted as False

  3. Lists are encoded as a comma separated string in example: "a,b,c, d" (intentionally put whitespace in there. It is allowed)

  4. Default Values are preffered to be set to the same type as the setting.

The type of the default value implicitly sets the datatype of the env var. May be overriden by setting return_type explicitly.

  • list -> [],
  • str -> ''
  • int -> 0
  • bool -> True or False

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

magicenv-0.1.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

magicenv-0.1.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file magicenv-0.1.2.tar.gz.

File metadata

  • Download URL: magicenv-0.1.2.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.10 Darwin/19.6.0

File hashes

Hashes for magicenv-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dec834b73c6f08160e96c26595f95251ed28bb6d392ed46a19b7bc419508cbaf
MD5 ea360204509601a2c7ebb549aa07b3e3
BLAKE2b-256 94da6859f79770648dc1567f4e7d11bdaa310b36c7236019e0e9f5d0586261e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicenv-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.10 Darwin/19.6.0

File hashes

Hashes for magicenv-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6392f9bcfcb1a26318219b1543467d6ed6c71f6205e33007e45177ba0063bf8d
MD5 9328b4c884edc46385e8461115f9d19a
BLAKE2b-256 6cc81616b024c4d310cedcae86a4b7c65c0fbfa45b575a9eb7a8eee995c58740

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