Skip to content

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 src/zenml/constants.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
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 src/zenml/constants.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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.

Raises:

Type Description
TypeError

In case the value of the environment variable is not of a valid type.

Source code in src/zenml/constants.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 src/zenml/constants.py
84
85
86
87
88
89
90
91
92
93
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 src/zenml/constants.py
72
73
74
75
76
77
78
79
80
81
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"]