src.fairreckitlib.core.config.config_option_param
This module contains functionality for configuration option parameters.
Classes:
ConfigSingleOptionParam: parameter that can be a single value from a known list of options.
ConfigMultiOptionParam: parameter that can be multiple values from a known list of options.
Functions:
create_bool_param: create boolean option parameter.
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 option parameters. 2 3Classes: 4 5 ConfigSingleOptionParam: parameter that can be a single value from a known list of options. 6 ConfigMultiOptionParam: parameter that can be multiple values from a known list of options. 7 8Functions: 9 10 create_bool_param: create boolean option parameter. 11 12This program has been developed by students from the bachelor Computer Science at 13Utrecht University within the Software Project course. 14© Copyright Utrecht University (Department of Information and Computing Sciences) 15""" 16 17from typing import Any, List, Tuple, Type, Union 18 19from .config_base_param import ConfigOptionParam 20 21 22class ConfigMultiOptionParam(ConfigOptionParam): 23 """Config Multi Option Parameter. 24 25 The default_value and all the options are expected to be strings or None. 26 The default_value list is expected to have at least one entry. 27 The default_value list is expected to be present in the list of available options. 28 """ 29 30 def __init__( 31 self, 32 name: str, 33 default_value: List[str], 34 options: List[str]): 35 """Construct the ConfigMultiOptionParam. 36 37 Args: 38 name: the name of the parameter. 39 default_value: the default option(s) of the parameter. 40 options: list of available options for the parameter. 41 """ 42 ConfigOptionParam.__init__(self, name, str, default_value, options) 43 44 def validate_value(self, value: Any) -> Tuple[bool, List[str], str]: 45 """Validate the specified value with the parameter. 46 47 Checks for None, type mismatch, duplicates and if the value is one of the allowed options. 48 49 Args: 50 value: the list of options to verify with the parameter. 51 52 Returns: 53 whether it was successful, the validated value and (optional) message. 54 """ 55 if value is None: 56 return False, self.default_value, \ 57 'no value(s) specified, expected one or more of: ' + str(self.options) + '.' 58 59 # type checks 60 if not isinstance(value, list): 61 return False, self.default_value, 'Expected list got '+ str(type(value)) + '.' 62 63 # list entries check 64 if len(value) == 0: 65 return False, self.default_value, 'Expected one or more list entries.' 66 67 # split options into correct/incorrect/duplicate lists 68 incorrect, correct, duplicates = [], [], [] 69 valid_types = (self.value_type, type(None)) if None in self.options else self.value_type 70 for val in value: 71 # check val is correct 72 if isinstance(val, valid_types) and val in self.options: 73 # check val is duplicate 74 (duplicates if val in correct else correct).append(val) 75 # check val is incorrect 76 else: 77 incorrect.append(val) 78 79 # create error message from incorrect/duplicate lists 80 error = '' 81 if len(duplicates) > 0: 82 error = 'Duplicates: ' + str(duplicates) + '. ' 83 if len(incorrect) > 0: 84 error = error + 'Invalidated: ' + str(incorrect) + '. ' 85 86 # check if one or more values are correct 87 if len(correct) == 0: 88 return False, self.default_value, 'Expected at least one correct value. ' + error 89 90 return True, correct, error 91 92 93class ConfigSingleOptionParam(ConfigOptionParam): 94 """Config Single Option Parameter. 95 96 The default_value and all the options are expected to be either strings or booleans. 97 The default_value is expected to be present in the list of available options. 98 The options list is expected to have unique values. 99 """ 100 101 def __init__( 102 self, 103 name: str, 104 value_type: Type, 105 default_value: Union[str, bool], 106 options: Union[List[str], List[bool]]): 107 """Construct the ConfigOptionParam. 108 109 Args: 110 name: the name of the parameter. 111 value_type: the type of the parameter. 112 default_value: the default option of the parameter. 113 options: list of available options for the parameter. 114 """ 115 ConfigOptionParam.__init__(self, name, value_type, default_value, options) 116 117 def validate_value(self, value: Any) -> Tuple[bool, Union[str, bool], str]: 118 """Validate the specified value with the parameter. 119 120 Checks for None, type mismatch and if the value is one of the allowed options. 121 122 Args: 123 value: the option to verify with the parameter. 124 125 Returns: 126 whether it was successful, the validated value and (optional) message. 127 """ 128 if value is None: 129 return False, self.default_value, 'No value specified for \'' + \ 130 self.name + '\', expected one of: ' + str(self.options) + '.' 131 132 # type check 133 if not isinstance(value, self.value_type): 134 return False, self.default_value, \ 135 'Expected ' + str(self.value_type) + ' got '+ str(type(value)) + '.' 136 137 # value check 138 if value not in self.options: 139 return False, self.default_value, 'Expected one of: ' + str(self.options) 140 141 return True, value, '' 142 143 144def create_bool_param(name: str, default_value: bool) -> ConfigSingleOptionParam: 145 """Create a boolean option parameter. 146 147 Args: 148 name: the name of the boolean parameter. 149 default_value: the default boolean value. 150 151 Returns: 152 the boolean parameter. 153 """ 154 return ConfigSingleOptionParam(name, bool, default_value, [True, False])
23class ConfigMultiOptionParam(ConfigOptionParam): 24 """Config Multi Option Parameter. 25 26 The default_value and all the options are expected to be strings or None. 27 The default_value list is expected to have at least one entry. 28 The default_value list is expected to be present in the list of available options. 29 """ 30 31 def __init__( 32 self, 33 name: str, 34 default_value: List[str], 35 options: List[str]): 36 """Construct the ConfigMultiOptionParam. 37 38 Args: 39 name: the name of the parameter. 40 default_value: the default option(s) of the parameter. 41 options: list of available options for the parameter. 42 """ 43 ConfigOptionParam.__init__(self, name, str, default_value, options) 44 45 def validate_value(self, value: Any) -> Tuple[bool, List[str], str]: 46 """Validate the specified value with the parameter. 47 48 Checks for None, type mismatch, duplicates and if the value is one of the allowed options. 49 50 Args: 51 value: the list of options to verify with the parameter. 52 53 Returns: 54 whether it was successful, the validated value and (optional) message. 55 """ 56 if value is None: 57 return False, self.default_value, \ 58 'no value(s) specified, expected one or more of: ' + str(self.options) + '.' 59 60 # type checks 61 if not isinstance(value, list): 62 return False, self.default_value, 'Expected list got '+ str(type(value)) + '.' 63 64 # list entries check 65 if len(value) == 0: 66 return False, self.default_value, 'Expected one or more list entries.' 67 68 # split options into correct/incorrect/duplicate lists 69 incorrect, correct, duplicates = [], [], [] 70 valid_types = (self.value_type, type(None)) if None in self.options else self.value_type 71 for val in value: 72 # check val is correct 73 if isinstance(val, valid_types) and val in self.options: 74 # check val is duplicate 75 (duplicates if val in correct else correct).append(val) 76 # check val is incorrect 77 else: 78 incorrect.append(val) 79 80 # create error message from incorrect/duplicate lists 81 error = '' 82 if len(duplicates) > 0: 83 error = 'Duplicates: ' + str(duplicates) + '. ' 84 if len(incorrect) > 0: 85 error = error + 'Invalidated: ' + str(incorrect) + '. ' 86 87 # check if one or more values are correct 88 if len(correct) == 0: 89 return False, self.default_value, 'Expected at least one correct value. ' + error 90 91 return True, correct, error
Config Multi Option Parameter.
The default_value and all the options are expected to be strings or None. The default_value list is expected to have at least one entry. The default_value list is expected to be present in the list of available options.
31 def __init__( 32 self, 33 name: str, 34 default_value: List[str], 35 options: List[str]): 36 """Construct the ConfigMultiOptionParam. 37 38 Args: 39 name: the name of the parameter. 40 default_value: the default option(s) of the parameter. 41 options: list of available options for the parameter. 42 """ 43 ConfigOptionParam.__init__(self, name, str, default_value, options)
Construct the ConfigMultiOptionParam.
Args: name: the name of the parameter. default_value: the default option(s) of the parameter. options: list of available options for the parameter.
45 def validate_value(self, value: Any) -> Tuple[bool, List[str], str]: 46 """Validate the specified value with the parameter. 47 48 Checks for None, type mismatch, duplicates and if the value is one of the allowed options. 49 50 Args: 51 value: the list of options to verify with the parameter. 52 53 Returns: 54 whether it was successful, the validated value and (optional) message. 55 """ 56 if value is None: 57 return False, self.default_value, \ 58 'no value(s) specified, expected one or more of: ' + str(self.options) + '.' 59 60 # type checks 61 if not isinstance(value, list): 62 return False, self.default_value, 'Expected list got '+ str(type(value)) + '.' 63 64 # list entries check 65 if len(value) == 0: 66 return False, self.default_value, 'Expected one or more list entries.' 67 68 # split options into correct/incorrect/duplicate lists 69 incorrect, correct, duplicates = [], [], [] 70 valid_types = (self.value_type, type(None)) if None in self.options else self.value_type 71 for val in value: 72 # check val is correct 73 if isinstance(val, valid_types) and val in self.options: 74 # check val is duplicate 75 (duplicates if val in correct else correct).append(val) 76 # check val is incorrect 77 else: 78 incorrect.append(val) 79 80 # create error message from incorrect/duplicate lists 81 error = '' 82 if len(duplicates) > 0: 83 error = 'Duplicates: ' + str(duplicates) + '. ' 84 if len(incorrect) > 0: 85 error = error + 'Invalidated: ' + str(incorrect) + '. ' 86 87 # check if one or more values are correct 88 if len(correct) == 0: 89 return False, self.default_value, 'Expected at least one correct value. ' + error 90 91 return True, correct, error
Validate the specified value with the parameter.
Checks for None, type mismatch, duplicates and if the value is one of the allowed options.
Args: value: the list of options to verify with the parameter.
Returns: whether it was successful, the validated value and (optional) message.
Inherited Members
94class ConfigSingleOptionParam(ConfigOptionParam): 95 """Config Single Option Parameter. 96 97 The default_value and all the options are expected to be either strings or booleans. 98 The default_value is expected to be present in the list of available options. 99 The options list is expected to have unique values. 100 """ 101 102 def __init__( 103 self, 104 name: str, 105 value_type: Type, 106 default_value: Union[str, bool], 107 options: Union[List[str], List[bool]]): 108 """Construct the ConfigOptionParam. 109 110 Args: 111 name: the name of the parameter. 112 value_type: the type of the parameter. 113 default_value: the default option of the parameter. 114 options: list of available options for the parameter. 115 """ 116 ConfigOptionParam.__init__(self, name, value_type, default_value, options) 117 118 def validate_value(self, value: Any) -> Tuple[bool, Union[str, bool], str]: 119 """Validate the specified value with the parameter. 120 121 Checks for None, type mismatch and if the value is one of the allowed options. 122 123 Args: 124 value: the option to verify with the parameter. 125 126 Returns: 127 whether it was successful, the validated value and (optional) message. 128 """ 129 if value is None: 130 return False, self.default_value, 'No value specified for \'' + \ 131 self.name + '\', expected one of: ' + str(self.options) + '.' 132 133 # type check 134 if not isinstance(value, self.value_type): 135 return False, self.default_value, \ 136 'Expected ' + str(self.value_type) + ' got '+ str(type(value)) + '.' 137 138 # value check 139 if value not in self.options: 140 return False, self.default_value, 'Expected one of: ' + str(self.options) 141 142 return True, value, ''
Config Single Option Parameter.
The default_value and all the options are expected to be either strings or booleans. The default_value is expected to be present in the list of available options. The options list is expected to have unique values.
102 def __init__( 103 self, 104 name: str, 105 value_type: Type, 106 default_value: Union[str, bool], 107 options: Union[List[str], List[bool]]): 108 """Construct the ConfigOptionParam. 109 110 Args: 111 name: the name of the parameter. 112 value_type: the type of the parameter. 113 default_value: the default option of the parameter. 114 options: list of available options for the parameter. 115 """ 116 ConfigOptionParam.__init__(self, name, value_type, default_value, options)
Construct the ConfigOptionParam.
Args: name: the name of the parameter. value_type: the type of the parameter. default_value: the default option of the parameter. options: list of available options for the parameter.
118 def validate_value(self, value: Any) -> Tuple[bool, Union[str, bool], str]: 119 """Validate the specified value with the parameter. 120 121 Checks for None, type mismatch and if the value is one of the allowed options. 122 123 Args: 124 value: the option to verify with the parameter. 125 126 Returns: 127 whether it was successful, the validated value and (optional) message. 128 """ 129 if value is None: 130 return False, self.default_value, 'No value specified for \'' + \ 131 self.name + '\', expected one of: ' + str(self.options) + '.' 132 133 # type check 134 if not isinstance(value, self.value_type): 135 return False, self.default_value, \ 136 'Expected ' + str(self.value_type) + ' got '+ str(type(value)) + '.' 137 138 # value check 139 if value not in self.options: 140 return False, self.default_value, 'Expected one of: ' + str(self.options) 141 142 return True, value, ''
Validate the specified value with the parameter.
Checks for None, type mismatch and if the value is one of the allowed options.
Args: value: the option to verify with the parameter.
Returns: whether it was successful, the validated value and (optional) message.
Inherited Members
145def create_bool_param(name: str, default_value: bool) -> ConfigSingleOptionParam: 146 """Create a boolean option parameter. 147 148 Args: 149 name: the name of the boolean parameter. 150 default_value: the default boolean value. 151 152 Returns: 153 the boolean parameter. 154 """ 155 return ConfigSingleOptionParam(name, bool, default_value, [True, False])
Create a boolean option parameter.
Args: name: the name of the boolean parameter. default_value: the default boolean value.
Returns: the boolean parameter.