rics.env.read#
Read environment variables as specific types.
To read typing.Literal values, use rics.types.LiteralHelper.read_env() instead.
Functions
|
Read |
|
Read |
|
Read and convert an environment variable. |
|
Read |
|
Read |
|
Read |
- read_bool(var: str, default: bool = False, *, strict: bool = True, split: str) list[bool][source]#
- read_bool(var: str, default: bool = False, *, strict: bool = True, split: None = None) bool
Read
boolvariable.- Parameters:
var – Variable to read.
default – Default value to use when var is not set (or blank).
strict – If
False, always fall back to default instead of raising.split – Character to split on. Returns
list[bool]when set.
- Returns:
A
boolvalue, or a list thereof (if split is set).
Notes
See
str_as_bool()for value mapping.Examples
Basic usage.
>>> import os >>> os.environ["MY_BOOL"] = "true" >>> read_bool("MY_BOOL") True
>>> os.environ["MY_BOOL"] = "0" >>> read_bool("MY_BOOL") False
When using
strict=False, unmapped values are converted to the default instead of raising.>>> os.environ["MY_BOOL"] = "not-a-bool" >>> read_bool("MY_BOOL", default=True, strict=False) True
When using split, elements are cleaned individually and blank items are skipped.
>>> os.environ["MY_BOOL_LIST"] = "true, 0, no, yes, enable,, false" >>> read_bool("MY_BOOL_LIST", split=",") [True, False, False, True, True, False]
Conversion (see
str_as_bool) must succeed for all elements, or the default will be returned.
- read_enum(var: str, default_or_type: EnumT | type[EnumT], *, split: str) list['EnumT'][source]#
- read_enum(var: str, default_or_type: EnumT | type[EnumT], *, split: None = None) EnumT
Read
enumvariable.This function wraps
LiteralHelper.from_enum()andLiteralHelper.read_env().- Parameters:
var – Variable to read.
default_or_type – Enum type or default enum value. Must be a concrete value (e.g.
Animal.doginstead of justAnimal) to allow unset or blank var values.split – Character to split on. Returns
list[EnumT]when set.
- Returns:
An
EnumTvalue, or a list thereof (if split is set).- Raises:
TypeError – If conversion fails.
Examples
Basic usage.
>>> from enum import Enum >>> class Animal(Enum): ... cat = "meow" ... dog = "woof"
Enums are matched by name, never by value.
>>> import os >>> os.environ["MY_ANIMAL"] = "cat" >>> read_enum("MY_ANIMAL", Animal) <Animal.cat: 'meow'>
Input is always cleaned and normalized.
>>> os.environ["MY_ANIMAL"] = " DOG" >>> read_enum("MY_ANIMAL", Animal) <Animal.dog: 'woof'>
This also applies when reading multiple enums.
>>> os.environ["MY_ANIMAL_LIST"] = " DOG, cat , CAT" >>> read_enum("MY_ANIMAL_LIST", Animal, split=",") [<Animal.dog: 'woof'>, <Animal.cat: 'meow'>, <Animal.cat: 'meow'>]
Unlike the other functions in this module,
read_enumalways runs in strict mode. Define a default for when the var is not set or blank by passing concrete value in the second position.>>> read_enum("MISSING_VARIABLE", Animal.dog) <Animal.dog: 'woof'>
Unknown values will always raise.
- read_env(var: str, converter: Callable[[str], T], default: T, *, strict: bool = True, type_name: str | None = None, split: None = None, catch: tuple[type[Exception], ...] | None = None) T[source]#
- read_env(var: str, converter: Callable[[str], T], default: T, *, strict: bool = True, type_name: str | None = None, split: str, catch: tuple[type[Exception], ...] | None = None) list[T]
Read and convert an environment variable.
- Parameters:
var – Variable to read.
converter – A callable
(str) -> T, where the argument is the environment variable value.default – Default value to use when var is not set (or blank).
strict – If
False, always fall back to default instead of raising.type_name – Used in error messages. Derive if
None.split – Character to split on. Returns
list[T]when set.catch – Types to suppress when calling
converter. Default is(ValueError, TypeError).
- Returns:
A
Tvalue, or a list thereof (if split is set).- Raises:
ValueError – If conversion fails.
Notes
If the variable key is unset or the value is empty, the default value is always returned.
- read_float(var: str, default: float = 0.0, *, strict: bool = True, split: str) list[float][source]#
- read_float(var: str, default: float = 0.0, *, strict: bool = True, split: None = None) float
Read
floatvariable.- Parameters:
var – Variable to read.
default – Default value to use when var is not set (or blank).
strict – If
False, always fall back to default instead of raising.split – Character to split on. Returns
list[float]when set.
- Returns:
An
floatvalue, or a list thereof (if split is set).
Examples
Basic usage.
>>> import os >>> os.environ["MY_FLOAT"] = "1.0" >>> read_float("MY_FLOAT") 1.0
When using
strict=False, unmapped values are converted to the default instead of raising.>>> os.environ["MY_FLOAT"] = "not-a-float" >>> read_float("MY_FLOAT", strict=False) 0.0
When using split, elements are cleaned individually and blank items are skipped.
>>> os.environ["MY_FLOAT_LIST"] = "0, 1.1, , 5" >>> read_float("MY_FLOAT_LIST", split=",") [0.0, 1.1, 5.0]
Conversion must succeed for all elements, or the default will be returned.
- read_int(var: str, default: int = 0, *, strict: bool = True, split: str) list[int][source]#
- read_int(var: str, default: int = 0, *, strict: bool = True, split: None = None) int
Read
intvariable.- Parameters:
var – Variable to read.
default – Default value to use when var is not set (or blank).
strict – If
False, always fall back to default instead of raising.split – Character to split on. Returns
list[int]when set.
- Returns:
An
intvalue, or a list thereof (if split is set).
Examples
Basic usage.
>>> import os >>> os.environ["MY_INT"] = "1" >>> read_int("MY_INT") 1
When using
strict=False, unmapped values are converted to the default instead of raising.>>> os.environ["MY_INT"] = "not-an-int" >>> read_int("MY_INT", strict=False) 0
Note that this includes passing
floatvalues, even if the decimal is zero.>>> os.environ["MY_INT"] = "0.0" >>> read_int("MY_INT", default=2019, strict=False) 2019
When using split, elements are cleaned individually and blank items are skipped.
>>> os.environ["MY_INT_LIST"] = "0, 1, , 5" >>> read_int("MY_INT_LIST", split=",") [0, 1, 5]
Conversion must succeed for all elements, or the default will be returned.
- read_str(var: str, default: str = '', *, strict: bool = True, split: str) list[str][source]#
- read_str(var: str, default: str = '', *, strict: bool = True, split: None = None) str
Read
strvariable.- Parameters:
var – Variable to read.
default – Default value to use when var is not set (or blank).
strict – If
False, always fall back to default instead of raising.split – Character to split on. Returns
list[str]when set.
- Returns:
An
strvalue, or a list thereof (if split is set).
Examples
Basic usage.
>>> import os >>> os.environ["MY_STR"] = " foo " >>> read_str("MY_STR") 'foo'
Input is stripped and cleaned, returning bare strings. Empty items are discarded when using split.
>>> os.environ["MY_STR_LIST"] = " foo, , bar " >>> read_str("MY_STR_LIST", split=",") ['foo', 'bar']
This means that input such as
', , , 'will return an empty list.