typed-environ
Environment variables with type support.
Usage
from typed_environ import environ
-
int
value without defaultport = int(environ['PORT'])
-
int
value with defaultport = environ.get('PORT', type=int, default=8000)
-
bool
value without defaultdebug = bool(environ['DEBUG'])
Note: The return value of
environ['DEBUG']
is not simply astr
, sincebool
cast for astr
only checks if the string is empty. Similarly, casting the result intolist
ordict
(see below) works differently from casting on a string. The return value can still be used as a string normally (excludingbool
anditer
calls). To avoid complications onstr
values, explicitly callstr(environ['xxx'])
, or useos.environ
instead. -
bool
value with defaultdebug = environ.get('DEBUG', type=bool, default=False)
-
list
allowed_hosts = environ.get('ALLOWED_HOSTS', type=list)
Split the string by
','
.Note 1: If the value is an empty string, returns
[]
instead of['']
.Note 2:
default
here isNone
. It does not have to be a value oftype
. -
json
data_list = list(environ['DATA_LIST']) data = dict(environ['DATA'])
Load the environment variable from a json-formatted string, which can be a
list
or adict
.Note: When calling
iter
(andlist
,dict
,tuple
,set
, etc. by extension), json-decoding will be attempted first, and then comma-splitting as specified above.
Installation
pip install typed-environ