API

class envy.Environment(environ)[source]

Class for reading and casting environment variables

This class presents the main interface for interacting with the environment. Once instantiated, it can either be called as a function, or any of the convenience methods can be used.

Parameters:environ (dict) – Environment to read variables from
__call__(var, default=<class 'envy.NoValue'>, cast=None, force=True)[source]

Function interface

Once the environment has been initialised, it can be called as a function. This is necessary to provide custom casting, or it can sometimes be preferred for consistency.

Examples

Casting an environment variable:

>>> env = Environment({'MY_VAR': '1'})
>>> env('MY_VAR', cast=int)
1

Providing a default:

>>> env = Environment({})
>>> env('ANOTHER_VAR', default='value')
"value"
Parameters:
  • var (str) – The name of the environment variable
  • default – The value to return if the environment variable does not exist
  • cast – type or function for casting environment variable. See casting
  • force (bool) – Whether to force casting of the default value
Returns:

The environment variable if it exists, otherwise default

Raises:

ImproperlyConfigured

__contains__(var)[source]

Test if an environment variable exists

Allows using the in operator to test if an environment variable exists.

Examples

>>> env = Environment({'MY_VAR': '1'})
>>> 'MY_VAR' in env
True
>>> 'ANOTHER_VAR' in env
False
bool(var, default=<class 'envy.NoValue'>, force=True)[source]

Convenience method for casting to a bool

decimal(var, default=<class 'envy.NoValue'>, force=True)[source]

Convenience method for casting to a decimal.Decimal

Note

Casting

dict(var, default=<class 'envy.NoValue'>, cast=None, force=True)[source]

Convenience method for casting to a dict

Note

Casting

float(var, default=<class 'envy.NoValue'>, force=True)[source]

Convenience method for casting to a float

int(var, default=<class 'envy.NoValue'>, force=True)[source]

Convenience method for casting to an int

json(var, default=<class 'envy.NoValue'>, force=True)[source]

Get environment variable, parsed as a json string

list(var, default=<class 'envy.NoValue'>, cast=None, force=True)[source]

Convenience method for casting to a list

Note

Casting

set(var, default=<class 'envy.NoValue'>, cast=None, force=True)[source]

Convenience method for casting to a set

Note

Casting

str(var, default=<class 'envy.NoValue'>, force=True)[source]

Convenience method for casting to a str

tuple(var, default=<class 'envy.NoValue'>, cast=None, force=True)[source]

Convenience method for casting to a tuple

Note

Casting

url(var, default=<class 'envy.NoValue'>, force=True)[source]

Get environment variable, parsed with urlparse/urllib.parse

Exceptions

class envy.ImproperlyConfigured[source]

Configuration Exception

Imported from Django if available, otherwise defined as a simple subclass of Exception