formelsammlung.envvar module¶
-
formelsammlung.envvar.getenv_typed(var_name, default=None, rv_type=None, *, raise_error_if_no_value=False, true_bool_values=None, false_bool_values=None)[source]¶ Wrap
os.getenv()to adjust the type of the values.Instead of returning the environments variable’s value as
strlikeos.getenv()you can setrv_typeto a type to convert the value to. Ifrv_typeis not set the type gets guessed and used for conversion.Guessable types are:
How to use:
>>> os.environ["TEST_ENV_VAR"] = "2" >>> getenv_typed("TEST_ENV_VAR", 1, int) 2
- Parameters
var_name (
str) – Name of the environment variable.default (
Optional[Any]) – Default value if no value is found forvar_name. Default:None.rv_type (
Optional[type]) – Type the value of the environment variable should be changed into. If not set or set toNonethe type gets guessed. Default:None.raise_error_if_no_value (
bool) – IfTrueraises anKeyErrorwhen no value is found forvar_nameanddefaultisNone. Parameter is keyword only. Default:Falsetrue_bool_values (
Optional[Iterable]) – Set of objects whose string representations are matched case-insensitive against the environment variable’s value if therv_typeisboolor the type gets guessed. If a match is foundTrueis returned. Parameter is keyword only. Default:(1, "y", "yes", "t", True)false_bool_values (
Optional[Iterable]) – Set of objects whose string representations are matched case-insensitive against the environment variable’s value if therv_typeisboolor the type gets guessed. If a match is foundFalseis returned. Parameter is keyword only. Default:(0, "n", "no", "f", False)
- Raises
- Return type
- Returns
Value for
var_nameordefaultconverted torv_typeor guessed type.