Read an environment variable and interpret it as a boolean. True values are (case insensitive): 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'. Args: varname: the name of the variable default: the default boolean value
(varname: str, default: bool)
| 22 | |
| 23 | |
| 24 | def bool_env(varname: str, default: bool) -> bool: |
| 25 | """Read an environment variable and interpret it as a boolean. |
| 26 | |
| 27 | True values are (case insensitive): 'y', 'yes', 't', 'true', 'on', and '1'; |
| 28 | false values are 'n', 'no', 'f', 'false', 'off', and '0'. |
| 29 | |
| 30 | Args: |
| 31 | varname: the name of the variable |
| 32 | default: the default boolean value |
| 33 | Raises: ValueError if the environment variable is anything else. |
| 34 | """ |
| 35 | val = os.getenv(varname, str(default)) |
| 36 | val = val.lower() |
| 37 | if val in ("y", "yes", "t", "true", "on", "1"): |
| 38 | return True |
| 39 | elif val in ("n", "no", "f", "false", "off", "0"): |
| 40 | return False |
| 41 | else: |
| 42 | raise ValueError(f"invalid truth value {val!r} for environment {varname!r}") |
| 43 | |
| 44 | |
| 45 | def int_env(varname: str, default: int) -> int: |
no test coverage detected