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
str
likeos.getenv()
you can setrv_type
to a type to convert the value to. Ifrv_type
is 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 toNone
the type gets guessed. Default:None
.raise_error_if_no_value (
bool
) – IfTrue
raises anKeyError
when no value is found forvar_name
anddefault
isNone
. Parameter is keyword only. Default:False
true_bool_values (
Optional
[Iterable
]) – Set of objects whose string representations are matched case-insensitive against the environment variable’s value if therv_type
isbool
or the type gets guessed. If a match is foundTrue
is 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_type
isbool
or the type gets guessed. If a match is foundFalse
is returned. Parameter is keyword only. Default:(0, "n", "no", "f", False)
- Raises
- Return type
- Returns
Value for
var_name
ordefault
converted torv_type
or guessed type.