src.config
Attributes
Functions
|
Reads an environment variable, with settings to allow defaults, empty values, and type casting |
|
Takes a string and casts it to necessary primitive builtin types. Tested with int, float, and bool. |
Module Contents
- src.config.T
- src.config.get_env_variable(var_name: str, default: T = None, allow_empty: bool = False, cast_to: Type[T] = str) T
Reads an environment variable, with settings to allow defaults, empty values, and type casting To read a boolean EXAMPLE_ENV_VAR=False use get_env_variable(“EXAMPLE_ENV_VAR”, cast_to=bool)
- Parameters:
var_name (str) – The name of the environment variable to retrieve.
default (T = None) – Default return value if the environment variable does not exist. Doesn’t override empty string vars.
allow_empty (bool) – If False then a KeyError will be raised if the environment variable is empty.
cast_to (Type[T]) – The type to cast to e.g. str, int, or bool
- Return type:
The environment variable, or default if it does not exist, as type T.
- Raises:
KeyError – If allow_empty is False and the environment variable is empty string or None
ValueError – If cast_to is not compatible with the value stored.
- src.config._cast_str(str_to_cast: str, cast_to: Type[T]) T
Takes a string and casts it to necessary primitive builtin types. Tested with int, float, and bool. For bool, this detects if the value is in the case-insensitive sets {“True”, “T”, “1”} or {“False”, “F”, “0”} and raises a ValueError if not. For example _cast_str(“False”, bool) -> False
- Parameters:
str_to_cast (str) – The string that is going to be casted to the type
cast_to (Type[T]) – The type to cast to e.g. bool
- Return type:
The string casted to type T defined by cast_to.
- Raises:
ValueError if [cast_to] is not compatible with the value stored. –