Constants
zenml.constants
ZenML constants.
handle_bool_env_var(var, default=False)
Converts normal env var to boolean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var |
str |
The environment variable to convert. |
required |
default |
bool |
The default value to return if the env var is not set. |
False |
Returns:
Type | Description |
---|---|
bool |
The converted value. |
Source code in zenml/constants.py
def handle_bool_env_var(var: str, default: bool = False) -> bool:
"""Converts normal env var to boolean.
Args:
var: The environment variable to convert.
default: The default value to return if the env var is not set.
Returns:
The converted value.
"""
value = os.getenv(var)
if is_true_string_value(value):
return True
elif is_false_string_value(value):
return False
return default
handle_int_env_var(var, default=0)
Converts normal env var to int.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var |
str |
The environment variable to convert. |
required |
default |
int |
The default value to return if the env var is not set. |
0 |
Returns:
Type | Description |
---|---|
int |
The converted value. |
Source code in zenml/constants.py
def handle_int_env_var(var: str, default: int = 0) -> int:
"""Converts normal env var to int.
Args:
var: The environment variable to convert.
default: The default value to return if the env var is not set.
Returns:
The converted value.
"""
value = os.getenv(var, "")
try:
return int(value)
except (ValueError, TypeError):
return default
handle_json_env_var(var, expected_type, default=None)
Converts a json env var into a Python object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var |
str |
The environment variable to convert. |
required |
default |
Optional[List[str]] |
The default value to return if the env var is not set. |
None |
expected_type |
Type[~T] |
The type of the expected Python object. |
required |
Returns:
Type | Description |
---|---|
Any |
The converted list value. |
Exceptions:
Type | Description |
---|---|
TypeError |
In case the value of the environment variable is not of a valid type. |
Source code in zenml/constants.py
def handle_json_env_var(
var: str,
expected_type: Type[T],
default: Optional[List[str]] = None,
) -> Any:
"""Converts a json env var into a Python object.
Args:
var: The environment variable to convert.
default: The default value to return if the env var is not set.
expected_type: The type of the expected Python object.
Returns:
The converted list value.
Raises:
TypeError: In case the value of the environment variable is not of a
valid type.
"""
# this needs to be here to avoid mutable defaults
if default is None:
default = []
value = os.getenv(var)
if value:
try:
loaded_value = json.loads(value)
# check if loaded value is of correct type
if expected_type is None or isinstance(
loaded_value, expected_type
):
return loaded_value
else:
raise TypeError # if not correct type, raise TypeError
except (TypeError, json.JSONDecodeError):
# Use raw logging to avoid cyclic dependency
logging.warning(
f"Environment Variable {var} could not be loaded, into type "
f"{expected_type}, defaulting to: {default}."
)
return default
else:
return default
is_false_string_value(value)
Checks if the given value is a string representation of 'False'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
the value to check. |
required |
Returns:
Type | Description |
---|---|
bool |
Whether the input value represents a string version of 'False'. |
Source code in zenml/constants.py
def is_false_string_value(value: Any) -> bool:
"""Checks if the given value is a string representation of 'False'.
Args:
value: the value to check.
Returns:
Whether the input value represents a string version of 'False'.
"""
return value in ["0", "n", "no", "False", "false"]
is_true_string_value(value)
Checks if the given value is a string representation of 'True'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
the value to check. |
required |
Returns:
Type | Description |
---|---|
bool |
Whether the input value represents a string version of 'True'. |
Source code in zenml/constants.py
def is_true_string_value(value: Any) -> bool:
"""Checks if the given value is a string representation of 'True'.
Args:
value: the value to check.
Returns:
Whether the input value represents a string version of 'True'.
"""
return value in ["1", "y", "yes", "True", "true"]