src.fairreckitlib.core.config.config_value_param

This module contains functionality for configuration value parameters.

Classes:

ConfigNumberParam: parameter that can be a number between a minimum and maximum.
ConfigRandomParam: parameter that can be used to pick the (optional) random seed.
ConfigRangeParam: parameter that can be a range between a minimum and maximum.

Functions:

validate_min_max:
validate_type:

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

  1"""This module contains functionality for configuration value parameters.
  2
  3Classes:
  4
  5    ConfigNumberParam: parameter that can be a number between a minimum and maximum.
  6    ConfigRandomParam: parameter that can be used to pick the (optional) random seed.
  7    ConfigRangeParam: parameter that can be a range between a minimum and maximum.
  8
  9Functions:
 10
 11    validate_min_max:
 12    validate_type:
 13
 14This program has been developed by students from the bachelor Computer Science at
 15Utrecht University within the Software Project course.
 16© Copyright Utrecht University (Department of Information and Computing Sciences)
 17"""
 18
 19import sys
 20from typing import Any, Dict, Optional, Tuple, Type, Union
 21
 22from .config_base_param import ConfigValueParam, PARAM_KEY_MIN, PARAM_KEY_MAX
 23
 24
 25class ConfigNumberParam(ConfigValueParam):
 26    """Config Number Parameter.
 27
 28    The value_type of default_value and min_max_value types are all expected to be either
 29    int or float, conversions between the two during validation is available.
 30    The default_value is expected to be between the min_max_value.
 31    The min_max_value is expected to have min_value <= max_value.
 32    """
 33
 34    def __init__(
 35            self,
 36            name: str,
 37            value_type: Type,
 38            default_value: Union[int, float],
 39            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
 40        """Construct the ConfigNumberParam.
 41
 42        Args:
 43            name: the name of the parameter.
 44            value_type: the type of the parameter.
 45            default_value: the default value of the parameter.
 46            min_max_value: tuple with the minimum and maximum value of the parameter.
 47        """
 48        ConfigValueParam.__init__(self, name, value_type, default_value, min_max_value)
 49
 50    def validate_value(self, value: Any) -> Tuple[bool, Union[int, float], str]:
 51        """Validate the specified value with the parameter.
 52
 53        Checks for None, type mismatch (conversion between int and float is allowed) and if
 54        the value is between the minimum and maximum value of the parameter.
 55
 56        Args:
 57            value: the number to verify with the parameter.
 58
 59        Returns:
 60            whether it was successful, the validated value and (optional) message.
 61        """
 62        if value is None:
 63            return False, self.default_value, 'No value specified for \'' + \
 64                   self.name  + '\', expected value between ' + \
 65                   str(self.min_value) + ' and ' + str(self.max_value)
 66
 67        # type check
 68        type_success, value, type_error = \
 69            validate_type(self.name, value, self.value_type, self.default_value)
 70
 71        # value check
 72        val_success, value, val_error = \
 73            validate_min_max(self.name, value, self.min_value, self.max_value)
 74
 75        return type_success and val_success, value, type_error + val_error
 76
 77
 78class ConfigRandomParam(ConfigNumberParam):
 79    """Config Random Parameter."""
 80
 81    def __init__(self, name: str):
 82        """Construct the ConfigRandomParam.
 83
 84        Args:
 85            name: the name of the random seed parameter.
 86        """
 87        ConfigNumberParam.__init__(self, name, int, None, (0, sys.maxsize))
 88
 89    def validate_value(self, value: Any) -> Tuple[bool, Optional[int], str]:
 90        """Validate the specified value with the parameter.
 91
 92        Checks are the same as ConfigNumberParam.validate_value, but None is allowed.
 93
 94        Args:
 95            value: the random seed to verify with the parameter.
 96
 97        Returns:
 98            whether it was successful, the validated value and (optional) message.
 99        """
100        # skips the 'None' error from ConfigNumberParam
101        if value is None:
102            return True, value, ''
103
104        return ConfigNumberParam.validate_value(self, value)
105
106
107class ConfigRangeParam(ConfigValueParam):
108    """Config Range Parameter.
109
110    The value_type of default_value and min_max_value types are all expected to be either
111    int or float, conversions between the two during validation is available.
112    The default_value min and max values are expected to be between the min_max_value.
113    The default_value and min_max_value are expected to have min_value <= max_value.
114    """
115
116    def __init__(
117            self,
118            name: str,
119            value_type: Type,
120            default_value: Union[Tuple[int, int], Tuple[float, float]],
121            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
122        """Construct the ConfigRangeParam.
123
124        Args:
125            name: the name of the parameter.
126            value_type: the type of the parameter.
127            default_value: tuple with the default minimum and maximum value of the parameter.
128            min_max_value: tuple with the minimum and maximum value of the parameter.
129        """
130        ConfigValueParam.__init__(
131            self,
132            name,
133            value_type,
134            {
135                PARAM_KEY_MIN: default_value[0],
136                PARAM_KEY_MAX: default_value[1]
137            },
138            min_max_value
139        )
140
141    def validate_value(self, value: Any) -> Tuple[bool, Dict[str, Union[int, float]], str]:
142        """Validate the specified value with the parameter.
143
144        Checks for None, type mismatch (conversion between int and float is allowed) and if
145        the min-max range is between the minimum and maximum value of the parameter.
146
147        Args:
148            value: the min-max range dictionary to verify with the parameter.
149
150        Returns:
151            whether it was successful, the validated value and (optional) message.
152        """
153        if value is None:
154            return False, self.default_value, 'No value specified, expected \'' + \
155                   PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\' range between ' + \
156                   str(self.min_value) + ' and ' + str(self.max_value)
157
158        # check min-max range dictionary
159        success, min_max_range, error = self.validate_dict(value)
160        # initialize result value with defaults
161        result_val = self.default_value
162
163        # check min-max range type
164        for key, val in min_max_range.items():
165            val_success, val, val_err = validate_type(key, val, self.value_type, result_val[key])
166            success = success and val_success
167            result_val[key] = val
168            error += val_err
169
170        # check min-max range value
171        for key, val in dict(result_val).items():
172            val_success, val, val_err = validate_min_max(key, val, self.min_value, self.max_value)
173            success = success and val_success
174            result_val[key] = val
175            error += val_err
176
177        # swap when min range > max range
178        if result_val[PARAM_KEY_MIN] > result_val[PARAM_KEY_MAX]:
179            result_val[PARAM_KEY_MIN], result_val[PARAM_KEY_MAX] = \
180            result_val[PARAM_KEY_MAX], result_val[PARAM_KEY_MIN]
181            success = False
182            error += 'Swapped \'' + PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\'. '
183
184        return success, result_val, error
185
186    def validate_dict(self, value: Any) -> Tuple[bool, Dict[str, Any], str]:
187        """Validate the specified value to a min-max range dictionary.
188
189        Args:
190            value: the min-max range dictionary to validate.
191
192        Returns:
193            whether it was successful, the validated dictionary and (optional) message.
194        """
195        if not isinstance(value, dict):
196            return False, self.default_value, \
197                   'Expected dictionary with \'min\' and \'max\' range between ' + \
198                   str(self.min_value) + ' and ' + str(self.max_value)
199
200        success = True
201        if PARAM_KEY_MIN in value:
202            min_error = ''
203            min_val = value[PARAM_KEY_MIN]
204        else:
205            min_val = self.min_value
206            min_error = 'Expected \'min\' in dictionary. '
207            success = False
208
209        if PARAM_KEY_MAX in value:
210            max_error = ''
211            max_val = value[PARAM_KEY_MAX]
212        else:
213            max_val = self.max_value
214            max_error = 'Expected \'max\' in dictionary. '
215            success = False
216
217        return success, { PARAM_KEY_MIN: min_val, PARAM_KEY_MAX: max_val }, min_error + max_error
218
219
220def validate_min_max(
221        value_name: str,
222        value: Union[int, float],
223        min_value: Union[int, float],
224        max_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
225    """Validate the value with the specified min- and max-value.
226
227    Args:
228        value_name: the name associated with the value.
229        value: the value to validate.
230        min_value: the minimum value to use for validation.
231        max_value: the maximum value to use for validation.
232
233    Returns:
234        whether it was successful, the validated value and (optional) message.
235    """
236    if value < min_value:
237        return False, min_value, 'Expected \'' + value_name + \
238               '\' greater or equal to ' + str(min_value) + '. '
239    if value > max_value:
240        return False, max_value, 'Expected \'' + value_name + \
241               '\' less or equal to ' + str(max_value) + '. '
242
243    return True, value, ''
244
245def validate_type(
246        value_name: str,
247        value: Any,
248        value_type: Type,
249        default_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
250    """Validate the value to be of the correct type.
251
252    Conversion between int/float is allowed and will return True, but
253    with the optional message that the value was cast to the correct value_type.
254
255    Args:
256        value_name: the name associated with the value.
257        value: the value to validate the type of.
258        value_type: the type of the value.
259        default_value: the default value to return on invalidation.
260
261    Returns:
262        whether it was successful, the validated value and (optional) message.
263    """
264    if isinstance(value, float) and value_type == int:
265        return True, value_type(value), 'Value \'' + value_name + '\' cast to int. '
266    if isinstance(value, int) and not isinstance(value, bool) and value_type == float:
267        return True, value_type(value), 'Value \'' + value_name + '\' cast to float. '
268    if not isinstance(value, value_type) or isinstance(value, bool):
269        return False, default_value, 'Expected \'' + value_name + '\' to be ' + \
270               str(value_type) + ' got ' + str(type(value)) + '. '
271
272    return True, value, ''
26class ConfigNumberParam(ConfigValueParam):
27    """Config Number Parameter.
28
29    The value_type of default_value and min_max_value types are all expected to be either
30    int or float, conversions between the two during validation is available.
31    The default_value is expected to be between the min_max_value.
32    The min_max_value is expected to have min_value <= max_value.
33    """
34
35    def __init__(
36            self,
37            name: str,
38            value_type: Type,
39            default_value: Union[int, float],
40            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
41        """Construct the ConfigNumberParam.
42
43        Args:
44            name: the name of the parameter.
45            value_type: the type of the parameter.
46            default_value: the default value of the parameter.
47            min_max_value: tuple with the minimum and maximum value of the parameter.
48        """
49        ConfigValueParam.__init__(self, name, value_type, default_value, min_max_value)
50
51    def validate_value(self, value: Any) -> Tuple[bool, Union[int, float], str]:
52        """Validate the specified value with the parameter.
53
54        Checks for None, type mismatch (conversion between int and float is allowed) and if
55        the value is between the minimum and maximum value of the parameter.
56
57        Args:
58            value: the number to verify with the parameter.
59
60        Returns:
61            whether it was successful, the validated value and (optional) message.
62        """
63        if value is None:
64            return False, self.default_value, 'No value specified for \'' + \
65                   self.name  + '\', expected value between ' + \
66                   str(self.min_value) + ' and ' + str(self.max_value)
67
68        # type check
69        type_success, value, type_error = \
70            validate_type(self.name, value, self.value_type, self.default_value)
71
72        # value check
73        val_success, value, val_error = \
74            validate_min_max(self.name, value, self.min_value, self.max_value)
75
76        return type_success and val_success, value, type_error + val_error

Config Number Parameter.

The value_type of default_value and min_max_value types are all expected to be either int or float, conversions between the two during validation is available. The default_value is expected to be between the min_max_value. The min_max_value is expected to have min_value <= max_value.

ConfigNumberParam( name: str, value_type: Type, default_value: Union[int, float], min_max_value: Union[Tuple[int, int], Tuple[float, float]])
35    def __init__(
36            self,
37            name: str,
38            value_type: Type,
39            default_value: Union[int, float],
40            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
41        """Construct the ConfigNumberParam.
42
43        Args:
44            name: the name of the parameter.
45            value_type: the type of the parameter.
46            default_value: the default value of the parameter.
47            min_max_value: tuple with the minimum and maximum value of the parameter.
48        """
49        ConfigValueParam.__init__(self, name, value_type, default_value, min_max_value)

Construct the ConfigNumberParam.

Args: name: the name of the parameter. value_type: the type of the parameter. default_value: the default value of the parameter. min_max_value: tuple with the minimum and maximum value of the parameter.

def validate_value(self, value: Any) -> Tuple[bool, Union[int, float], str]:
51    def validate_value(self, value: Any) -> Tuple[bool, Union[int, float], str]:
52        """Validate the specified value with the parameter.
53
54        Checks for None, type mismatch (conversion between int and float is allowed) and if
55        the value is between the minimum and maximum value of the parameter.
56
57        Args:
58            value: the number to verify with the parameter.
59
60        Returns:
61            whether it was successful, the validated value and (optional) message.
62        """
63        if value is None:
64            return False, self.default_value, 'No value specified for \'' + \
65                   self.name  + '\', expected value between ' + \
66                   str(self.min_value) + ' and ' + str(self.max_value)
67
68        # type check
69        type_success, value, type_error = \
70            validate_type(self.name, value, self.value_type, self.default_value)
71
72        # value check
73        val_success, value, val_error = \
74            validate_min_max(self.name, value, self.min_value, self.max_value)
75
76        return type_success and val_success, value, type_error + val_error

Validate the specified value with the parameter.

Checks for None, type mismatch (conversion between int and float is allowed) and if the value is between the minimum and maximum value of the parameter.

Args: value: the number to verify with the parameter.

Returns: whether it was successful, the validated value and (optional) message.

class ConfigRandomParam(ConfigNumberParam):
 79class ConfigRandomParam(ConfigNumberParam):
 80    """Config Random Parameter."""
 81
 82    def __init__(self, name: str):
 83        """Construct the ConfigRandomParam.
 84
 85        Args:
 86            name: the name of the random seed parameter.
 87        """
 88        ConfigNumberParam.__init__(self, name, int, None, (0, sys.maxsize))
 89
 90    def validate_value(self, value: Any) -> Tuple[bool, Optional[int], str]:
 91        """Validate the specified value with the parameter.
 92
 93        Checks are the same as ConfigNumberParam.validate_value, but None is allowed.
 94
 95        Args:
 96            value: the random seed to verify with the parameter.
 97
 98        Returns:
 99            whether it was successful, the validated value and (optional) message.
100        """
101        # skips the 'None' error from ConfigNumberParam
102        if value is None:
103            return True, value, ''
104
105        return ConfigNumberParam.validate_value(self, value)

Config Random Parameter.

ConfigRandomParam(name: str)
82    def __init__(self, name: str):
83        """Construct the ConfigRandomParam.
84
85        Args:
86            name: the name of the random seed parameter.
87        """
88        ConfigNumberParam.__init__(self, name, int, None, (0, sys.maxsize))

Construct the ConfigRandomParam.

Args: name: the name of the random seed parameter.

def validate_value(self, value: Any) -> Tuple[bool, Optional[int], str]:
 90    def validate_value(self, value: Any) -> Tuple[bool, Optional[int], str]:
 91        """Validate the specified value with the parameter.
 92
 93        Checks are the same as ConfigNumberParam.validate_value, but None is allowed.
 94
 95        Args:
 96            value: the random seed to verify with the parameter.
 97
 98        Returns:
 99            whether it was successful, the validated value and (optional) message.
100        """
101        # skips the 'None' error from ConfigNumberParam
102        if value is None:
103            return True, value, ''
104
105        return ConfigNumberParam.validate_value(self, value)

Validate the specified value with the parameter.

Checks are the same as ConfigNumberParam.validate_value, but None is allowed.

Args: value: the random seed to verify with the parameter.

Returns: whether it was successful, the validated value and (optional) message.

108class ConfigRangeParam(ConfigValueParam):
109    """Config Range Parameter.
110
111    The value_type of default_value and min_max_value types are all expected to be either
112    int or float, conversions between the two during validation is available.
113    The default_value min and max values are expected to be between the min_max_value.
114    The default_value and min_max_value are expected to have min_value <= max_value.
115    """
116
117    def __init__(
118            self,
119            name: str,
120            value_type: Type,
121            default_value: Union[Tuple[int, int], Tuple[float, float]],
122            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
123        """Construct the ConfigRangeParam.
124
125        Args:
126            name: the name of the parameter.
127            value_type: the type of the parameter.
128            default_value: tuple with the default minimum and maximum value of the parameter.
129            min_max_value: tuple with the minimum and maximum value of the parameter.
130        """
131        ConfigValueParam.__init__(
132            self,
133            name,
134            value_type,
135            {
136                PARAM_KEY_MIN: default_value[0],
137                PARAM_KEY_MAX: default_value[1]
138            },
139            min_max_value
140        )
141
142    def validate_value(self, value: Any) -> Tuple[bool, Dict[str, Union[int, float]], str]:
143        """Validate the specified value with the parameter.
144
145        Checks for None, type mismatch (conversion between int and float is allowed) and if
146        the min-max range is between the minimum and maximum value of the parameter.
147
148        Args:
149            value: the min-max range dictionary to verify with the parameter.
150
151        Returns:
152            whether it was successful, the validated value and (optional) message.
153        """
154        if value is None:
155            return False, self.default_value, 'No value specified, expected \'' + \
156                   PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\' range between ' + \
157                   str(self.min_value) + ' and ' + str(self.max_value)
158
159        # check min-max range dictionary
160        success, min_max_range, error = self.validate_dict(value)
161        # initialize result value with defaults
162        result_val = self.default_value
163
164        # check min-max range type
165        for key, val in min_max_range.items():
166            val_success, val, val_err = validate_type(key, val, self.value_type, result_val[key])
167            success = success and val_success
168            result_val[key] = val
169            error += val_err
170
171        # check min-max range value
172        for key, val in dict(result_val).items():
173            val_success, val, val_err = validate_min_max(key, val, self.min_value, self.max_value)
174            success = success and val_success
175            result_val[key] = val
176            error += val_err
177
178        # swap when min range > max range
179        if result_val[PARAM_KEY_MIN] > result_val[PARAM_KEY_MAX]:
180            result_val[PARAM_KEY_MIN], result_val[PARAM_KEY_MAX] = \
181            result_val[PARAM_KEY_MAX], result_val[PARAM_KEY_MIN]
182            success = False
183            error += 'Swapped \'' + PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\'. '
184
185        return success, result_val, error
186
187    def validate_dict(self, value: Any) -> Tuple[bool, Dict[str, Any], str]:
188        """Validate the specified value to a min-max range dictionary.
189
190        Args:
191            value: the min-max range dictionary to validate.
192
193        Returns:
194            whether it was successful, the validated dictionary and (optional) message.
195        """
196        if not isinstance(value, dict):
197            return False, self.default_value, \
198                   'Expected dictionary with \'min\' and \'max\' range between ' + \
199                   str(self.min_value) + ' and ' + str(self.max_value)
200
201        success = True
202        if PARAM_KEY_MIN in value:
203            min_error = ''
204            min_val = value[PARAM_KEY_MIN]
205        else:
206            min_val = self.min_value
207            min_error = 'Expected \'min\' in dictionary. '
208            success = False
209
210        if PARAM_KEY_MAX in value:
211            max_error = ''
212            max_val = value[PARAM_KEY_MAX]
213        else:
214            max_val = self.max_value
215            max_error = 'Expected \'max\' in dictionary. '
216            success = False
217
218        return success, { PARAM_KEY_MIN: min_val, PARAM_KEY_MAX: max_val }, min_error + max_error

Config Range Parameter.

The value_type of default_value and min_max_value types are all expected to be either int or float, conversions between the two during validation is available. The default_value min and max values are expected to be between the min_max_value. The default_value and min_max_value are expected to have min_value <= max_value.

ConfigRangeParam( name: str, value_type: Type, default_value: Union[Tuple[int, int], Tuple[float, float]], min_max_value: Union[Tuple[int, int], Tuple[float, float]])
117    def __init__(
118            self,
119            name: str,
120            value_type: Type,
121            default_value: Union[Tuple[int, int], Tuple[float, float]],
122            min_max_value: Union[Tuple[int, int], Tuple[float, float]]):
123        """Construct the ConfigRangeParam.
124
125        Args:
126            name: the name of the parameter.
127            value_type: the type of the parameter.
128            default_value: tuple with the default minimum and maximum value of the parameter.
129            min_max_value: tuple with the minimum and maximum value of the parameter.
130        """
131        ConfigValueParam.__init__(
132            self,
133            name,
134            value_type,
135            {
136                PARAM_KEY_MIN: default_value[0],
137                PARAM_KEY_MAX: default_value[1]
138            },
139            min_max_value
140        )

Construct the ConfigRangeParam.

Args: name: the name of the parameter. value_type: the type of the parameter. default_value: tuple with the default minimum and maximum value of the parameter. min_max_value: tuple with the minimum and maximum value of the parameter.

def validate_value(self, value: Any) -> Tuple[bool, Dict[str, Union[int, float]], str]:
142    def validate_value(self, value: Any) -> Tuple[bool, Dict[str, Union[int, float]], str]:
143        """Validate the specified value with the parameter.
144
145        Checks for None, type mismatch (conversion between int and float is allowed) and if
146        the min-max range is between the minimum and maximum value of the parameter.
147
148        Args:
149            value: the min-max range dictionary to verify with the parameter.
150
151        Returns:
152            whether it was successful, the validated value and (optional) message.
153        """
154        if value is None:
155            return False, self.default_value, 'No value specified, expected \'' + \
156                   PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\' range between ' + \
157                   str(self.min_value) + ' and ' + str(self.max_value)
158
159        # check min-max range dictionary
160        success, min_max_range, error = self.validate_dict(value)
161        # initialize result value with defaults
162        result_val = self.default_value
163
164        # check min-max range type
165        for key, val in min_max_range.items():
166            val_success, val, val_err = validate_type(key, val, self.value_type, result_val[key])
167            success = success and val_success
168            result_val[key] = val
169            error += val_err
170
171        # check min-max range value
172        for key, val in dict(result_val).items():
173            val_success, val, val_err = validate_min_max(key, val, self.min_value, self.max_value)
174            success = success and val_success
175            result_val[key] = val
176            error += val_err
177
178        # swap when min range > max range
179        if result_val[PARAM_KEY_MIN] > result_val[PARAM_KEY_MAX]:
180            result_val[PARAM_KEY_MIN], result_val[PARAM_KEY_MAX] = \
181            result_val[PARAM_KEY_MAX], result_val[PARAM_KEY_MIN]
182            success = False
183            error += 'Swapped \'' + PARAM_KEY_MIN + '\' and \'' + PARAM_KEY_MAX + '\'. '
184
185        return success, result_val, error

Validate the specified value with the parameter.

Checks for None, type mismatch (conversion between int and float is allowed) and if the min-max range is between the minimum and maximum value of the parameter.

Args: value: the min-max range dictionary to verify with the parameter.

Returns: whether it was successful, the validated value and (optional) message.

def validate_dict(self, value: Any) -> Tuple[bool, Dict[str, Any], str]:
187    def validate_dict(self, value: Any) -> Tuple[bool, Dict[str, Any], str]:
188        """Validate the specified value to a min-max range dictionary.
189
190        Args:
191            value: the min-max range dictionary to validate.
192
193        Returns:
194            whether it was successful, the validated dictionary and (optional) message.
195        """
196        if not isinstance(value, dict):
197            return False, self.default_value, \
198                   'Expected dictionary with \'min\' and \'max\' range between ' + \
199                   str(self.min_value) + ' and ' + str(self.max_value)
200
201        success = True
202        if PARAM_KEY_MIN in value:
203            min_error = ''
204            min_val = value[PARAM_KEY_MIN]
205        else:
206            min_val = self.min_value
207            min_error = 'Expected \'min\' in dictionary. '
208            success = False
209
210        if PARAM_KEY_MAX in value:
211            max_error = ''
212            max_val = value[PARAM_KEY_MAX]
213        else:
214            max_val = self.max_value
215            max_error = 'Expected \'max\' in dictionary. '
216            success = False
217
218        return success, { PARAM_KEY_MIN: min_val, PARAM_KEY_MAX: max_val }, min_error + max_error

Validate the specified value to a min-max range dictionary.

Args: value: the min-max range dictionary to validate.

Returns: whether it was successful, the validated dictionary and (optional) message.

def validate_min_max( value_name: str, value: Union[int, float], min_value: Union[int, float], max_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
221def validate_min_max(
222        value_name: str,
223        value: Union[int, float],
224        min_value: Union[int, float],
225        max_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
226    """Validate the value with the specified min- and max-value.
227
228    Args:
229        value_name: the name associated with the value.
230        value: the value to validate.
231        min_value: the minimum value to use for validation.
232        max_value: the maximum value to use for validation.
233
234    Returns:
235        whether it was successful, the validated value and (optional) message.
236    """
237    if value < min_value:
238        return False, min_value, 'Expected \'' + value_name + \
239               '\' greater or equal to ' + str(min_value) + '. '
240    if value > max_value:
241        return False, max_value, 'Expected \'' + value_name + \
242               '\' less or equal to ' + str(max_value) + '. '
243
244    return True, value, ''

Validate the value with the specified min- and max-value.

Args: value_name: the name associated with the value. value: the value to validate. min_value: the minimum value to use for validation. max_value: the maximum value to use for validation.

Returns: whether it was successful, the validated value and (optional) message.

def validate_type( value_name: str, value: Any, value_type: Type, default_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
246def validate_type(
247        value_name: str,
248        value: Any,
249        value_type: Type,
250        default_value: Union[int, float]) -> Tuple[bool, Union[int, float], str]:
251    """Validate the value to be of the correct type.
252
253    Conversion between int/float is allowed and will return True, but
254    with the optional message that the value was cast to the correct value_type.
255
256    Args:
257        value_name: the name associated with the value.
258        value: the value to validate the type of.
259        value_type: the type of the value.
260        default_value: the default value to return on invalidation.
261
262    Returns:
263        whether it was successful, the validated value and (optional) message.
264    """
265    if isinstance(value, float) and value_type == int:
266        return True, value_type(value), 'Value \'' + value_name + '\' cast to int. '
267    if isinstance(value, int) and not isinstance(value, bool) and value_type == float:
268        return True, value_type(value), 'Value \'' + value_name + '\' cast to float. '
269    if not isinstance(value, value_type) or isinstance(value, bool):
270        return False, default_value, 'Expected \'' + value_name + '\' to be ' + \
271               str(value_type) + ' got ' + str(type(value)) + '. '
272
273    return True, value, ''

Validate the value to be of the correct type.

Conversion between int/float is allowed and will return True, but with the optional message that the value was cast to the correct value_type.

Args: value_name: the name associated with the value. value: the value to validate the type of. value_type: the type of the value. default_value: the default value to return on invalidation.

Returns: whether it was successful, the validated value and (optional) message.