Skip to main content

A module for reading and writing environment variables.

Project description

A module for reading values from OS environment variables.

Compared to using os.getenv(), this module provides convenience functions, for parsing basic datatypes. It also allows specifying optional default values if the environment variable does not exist. Basic environment variable parsing and sanitizing is also performed.

Usage

import os
import envitro

# fails when environment variables are missing
bool_required = envitro.bool("BOOL_ENV")
int_required = envitro.int("INTEGER_ENV")
float_required = envitro.float("FLOAT_ENV")
str_required = envitro.str("STRING_ENV")

# basic sanitizing
os.environ["STR_ENV"] = "  var with spaces  "
envitro.str("STR_ENV") # returns "var with spaces"

# falls back to defaults
bool_default = envitro.bool("BOOL_NOT_FOUND", default=False)
int_default = envitro.int("INTEGER_NOT_FOUND", default=42)
float_default = envitro.float("FLOAT_NOT_FOUND", default=42.44)
str_default = envitro.str("STRING_NOT_FOUND", default="my_default")

# try multiple fallback ENV variables
os.environ["FALLBACK_ENV"] = "fallback_val"
single_fallback = envitro.str("MISSING", fallback="FALLBACK_ENV")
multiple_fallback = envitro.str("MISSING", fallback=["FALL_MISSING_1", "FALL_MISSING_2", "FALLBACK_ENV"])

# get and set raw environment variables
envitro.write("EXISTING_VAR", None) # clear the environment variable
envitro.write("RAW_STRING", " raw_string ")
envitro.read("RAW_STRING") # returns " raw_string "
envitro.read("MISSING_RAW_STRING", default=" defaultval ") # returns " defaultval "

# lists/tuples
os.environ["LIST_ENV"] = "item1,item2,item3"
list_required = envitro.list("LIST_ENV") # returns ["item1", "item2", "item3"]
tuple_required = envitro.tuple("LIST_ENV") # returns ("item1", "item2", "item3")
os.environ["LIST_ENV2"] = "item1;item2;item3"
list_required2 = envitro.list("LIST_ENV2", separator=";") # returns ["item1", "item2", "item3"]
tuple_required2 = envitro.tuple("LIST_ENV2", separator=";") # returns ("item1", "item2", "item3")

# utility functions
envitro.isset("MAYBE_SET_VARIABLE") # return True/False

Decorators

There are also decorators available to selectively enable or disable functions based on environment variables.

import envitro

@envitro.decorators.isset('ALLOW_FOO')
def foo():
    return "Hello World"

envitro.write('ALLOW_FOO', '1')
foo() # allowed

envitro.write('ALLOW_FOO', None)
foo() # return "None" and is not executed

@envitro.decorators.bool('ALLOW_REMOTE')
def get_remote(arg1, arg2):
    return call_remote_service(arg1, arg2)

envitro.write('ALLOW_REMOTE', 'True')
get_remote('hello', 'world') # calls remote service

envitro.write('ALLOW_REMOTE', 'False')
get_remote('hello', 'world') # returns "None" and is not executed

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

envitro-0.5.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

envitro-0.5.0-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file envitro-0.5.0.tar.gz.

File metadata

  • Download URL: envitro-0.5.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for envitro-0.5.0.tar.gz
Algorithm Hash digest
SHA256 8f4e50fad92617cf0a250871cced27d55d6bc9dcc0bdfc596142f8c8b90b07ee
MD5 8061531e73572bb9a883f8dc968203cf
BLAKE2b-256 cd8031f5cf6623a6b1433a056fa68bbb4f6777733ea7e4979a24fe0f9f62c3b9

See more details on using hashes here.

File details

Details for the file envitro-0.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for envitro-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aa58d175d7899b13a744090fe9456855a247f7035cff07c54175f14e3c5b6755
MD5 e9ab535c251e0d82ae28e7b046b52bff
BLAKE2b-256 66906d1ef21f4675d0664f7100e8de1b34f4cd12a57e7097000032f5fa5ca005

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page