API Reference¶
- class envanter.EnvironmentParser¶
Parses environment variables.
- bool(name: str, /, default: T | NotSet = NotSet.notset) bool | T¶
Get a boolean from environment variable. Allowed values are:
true,1,falseand0.- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyErrorValueError- Parser:
N/A. Checks if the value is in allowed values specified above.
- Returns:
A boolean or the default value.
- choice(name: str, /, default: V | NotSet = notset, *, choices: Iterable[str]) str | V¶
- choice(name: str, /, default: V | NotSet = notset, *, choices: Iterable[str], parser: Callable[[...], T]) T | V
Get an environment variable, provided that it complies with the choices in the related parameter. Otherwise, throws an exception (ValueError), with the available choices.
- Parameters:
name – Name of the environment variable.
default –
Optional default value in case the variable is not found.
Warning
If the variable is found but, it does not match any of the choices, an exception (
ValueError) will be raised.choices – Iterable of strings that contain the valid choices.
parser – A callable object that is going to parse the variable, optional.
- Raises:
KeyErrorValueError- Parser:
strif no custom parser is specified.- Returns:
The parsed value of environment variable.
- decimal(name: str, /, default: T | NotSet = NotSet.notset) Decimal | T¶
Get a decimal (
decimal.Decimal) from environment.- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyError- Parser:
decimal.Decimal- Returns:
A decimal or the default value.
- float(name: str, /, default: T | NotSet = NotSet.notset) float | T¶
Get a float from environment.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyError- Parser:
float- Returns:
A float or the default value.
- int(name: str, /, default: T | NotSet = NotSet.notset) int | T¶
Get an integer from environment.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyError- Parser:
int- Returns:
An integer or the default value.
- json(name: str, /, default: T | NotSet = NotSet.notset) Any | T¶
Get Python serialization of a JSON string from environment.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyError- Parser:
json.loads- Returns:
The Python serialized version of the JSON, or the default value.
- list(name: str, /, default: V | NotSet = notset, *, delimiter: str = ',') List[str] | V¶
- list(name: str, /, default: V | NotSet = notset, *, delimiter: str = ',', parser: Callable[[...], T]) List[T] | V
Derive a list from the value of an environment variable.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
delimiter – Specify a string with which the variable will be separated. By default, a comma is used, for example ‘hello,world’ would yield a list with 2 members.
parser – Specify a parser callable, which will be mapped to resulting list. For example, if you are expecting a list of integers, you may pass the
intfunction.
- Raises:
KeyError- Parser:
strif no custom parser is specified.- Returns:
A list of strings or the default value.
- parse(name: str, /, default: V | NotSet = NotSet.notset, *, parser: Callable[[...], T]) T | V¶
Allows fetching values from environment with custom parser function to modify it before returning.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
parser – A callable object that is going to parse the variable.
- Raises:
KeyError- Returns:
Parsed environment variable, or the default value.
- str(name: str, /, default: T | NotSet = NotSet.notset) str | T¶
Get a string from environment.
- Parameters:
name – Name of the environment variable.
default – Optional default value in case the variable is not found.
- Raises:
KeyError- Parser:
N/A.
- Returns:
A string or the default value.