src.fairreckitlib.core.config.config_parameters
This module contains a container of configuration parameters.
Classes:
ConfigParameters: container that stores multiple ConfigParam's.
Functions:
create_empty_parameters: create parameters with no entries.
create_params_random_seed: create configuration with only a random seed 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 a container of configuration parameters. 2 3Classes: 4 5 ConfigParameters: container that stores multiple ConfigParam's. 6 7Functions: 8 9 create_empty_parameters: create parameters with no entries. 10 create_params_random_seed: create configuration with only a random seed 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, Dict, List, Optional, Tuple, Type, Union 18 19from ..core_constants import KEY_RANDOM_SEED 20from .config_base_param import ConfigParam, PARAM_KEY_OPTIONS, PARAM_KEY_VALUES 21from .config_option_param import ConfigSingleOptionParam, ConfigMultiOptionParam, create_bool_param 22from .config_value_param import ConfigNumberParam, ConfigRandomParam, ConfigRangeParam 23 24 25class ConfigParameters: 26 """Config Parameters. 27 28 Container with varying Config parameters using a dictionary. 29 Moreover, he added option/value parameters are stored separately. 30 31 Public methods: 32 33 add_bool 34 add_multi_option 35 add_single_option 36 add_number 37 add_random_seed 38 add_range 39 get_defaults 40 get_num_params 41 get_param 42 get_param_names 43 to_dict 44 """ 45 46 def __init__(self): 47 """Construct the ConfigParameters.""" 48 self.params = {} 49 self.options = [] 50 self.values = [] 51 52 def add_bool(self, name: str, default_value: bool) -> None: 53 """Add a boolean parameter. 54 55 Sugar for an option parameter that is either True or False. 56 Raises a KeyError when the name of the parameter is already present. 57 58 Args: 59 name: name of the boolean parameter. 60 default_value: the default boolean value. 61 """ 62 param = create_bool_param(name, default_value) 63 64 self.add_param(param) 65 self.options.append(param) 66 67 def add_multi_option( 68 self, 69 name: str, 70 default_value: List[str], 71 options: List[str]) -> None: 72 """Add a multi option parameter. 73 74 The default_value and all the options are expected to be strings or None. 75 The default_value list is expected to have at least one entry. 76 The default_value list is expected to be present in the list of available options. 77 Raises a KeyError when the name of the parameter is already present. 78 79 Args: 80 name: the name of the parameter. 81 default_value: the default option(s) of the parameter. 82 options: list of available options for the parameter. 83 """ 84 param = ConfigMultiOptionParam( 85 name, 86 default_value, 87 options 88 ) 89 90 self.add_param(param) 91 self.options.append(param) 92 93 def add_single_option( 94 self, 95 name: str, 96 value_type: Type, 97 default_option: str, 98 options: List[str]) -> None: 99 """Add a single option parameter. 100 101 The default_value and all the options are expected to be strings. 102 The default_value is expected to be present in the list of available options. 103 The options list is expected to have unique values. 104 Raises a KeyError when the name of the parameter is already present. 105 106 Args: 107 name: the name of the parameter. 108 value_type: the type of the parameter. 109 default_option: default option of the parameter. 110 options: list of available options for the parameter. 111 """ 112 param = ConfigSingleOptionParam( 113 name, 114 value_type, 115 default_option, 116 options 117 ) 118 119 self.add_param(param) 120 self.options.append(param) 121 122 def add_number( 123 self, 124 name: str, 125 value_type: Type, 126 default_value: Union[int, float], 127 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 128 """Add a number parameter. 129 130 The value_type of default_value and min_max_value types are all expected to be either 131 int or float, conversions between the two during validation is available. 132 The default value is expected to be between the min_max_value. 133 The min_max_value is expected to have min_value <= max_value. 134 Raises a KeyError when the name of the parameter is already present. 135 136 Args: 137 name: the name of the parameter. 138 value_type: the type of the parameter. 139 default_value: the default value of the parameter. 140 min_max_value: tuple with the minimum and maximum value of the parameter. 141 """ 142 param = ConfigNumberParam( 143 name, 144 value_type, 145 default_value, 146 min_max_value 147 ) 148 149 self.add_param(param) 150 self.values.append(param) 151 152 def add_random_seed(self, name: str) -> None: 153 """Add a random seed parameter. 154 155 An integer number parameter, where the default_value is None. 156 Raises a KeyError when the name of the parameter is already present. 157 158 Args: 159 name: the name of the random seed parameter. 160 """ 161 param = ConfigRandomParam(name) 162 163 self.add_param(param) 164 self.values.append(param) 165 166 def add_range( 167 self, 168 name: str, 169 value_type: Type, 170 default_value: Union[Tuple[int, int], Tuple[float, float]], 171 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 172 """Add a range parameter. 173 174 The value_type of default_value and min_max_value types are all expected to be either 175 int or float, conversions between the two during validation is available. 176 The default_value min and max values are expected to be between the min_max_value. 177 The default_value and min_max_value are expected to have min_value <= max_value. 178 Raises a KeyError when the name of the parameter is already present. 179 180 Args: 181 name: the name of the parameter. 182 value_type: the type of the parameter. 183 default_value: tuple with the default minimum and maximum value of the parameter. 184 min_max_value: tuple with the minimum and maximum value of the parameter. 185 """ 186 param = ConfigRangeParam( 187 name, 188 value_type, 189 default_value, 190 min_max_value 191 ) 192 193 self.add_param(param) 194 self.values.append(param) 195 196 def get_defaults(self) -> Dict[str, Any]: 197 """Get the default values from all parameters. 198 199 Returns: 200 a dictionary containing name-default pairs for all parameters. 201 """ 202 defaults = {} 203 204 for param_name, param in self.params.items(): 205 defaults[param_name] = param.default_value 206 207 return defaults 208 209 def get_num_params(self) -> int: 210 """Get the number of parameters. 211 212 Returns: 213 the parameter count. 214 """ 215 return len(self.params) 216 217 def get_param(self, param_name: str) -> Optional[ConfigParam]: 218 """Get the parameter with the specified name. 219 220 Returns: 221 the parameter on success or None on failure. 222 """ 223 return self.params.get(param_name) 224 225 def get_param_names(self) -> List[str]: 226 """Get the names of all parameters. 227 228 Returns: 229 a list of all parameter names. 230 """ 231 param_names = [] 232 233 for param_name, _ in self.params.items(): 234 param_names.append(param_name) 235 236 return param_names 237 238 def to_dict(self) -> Dict[str, List[Dict[str, Any]]]: 239 """Get a dictionary describing all the parameters. 240 241 The parameters in the dictionary are stored separately: 242 PARAM_KEY_OPTIONS: list of all option parameter descriptions. 243 PARAM_KEY_VALUES: list of all value parameter descriptions. 244 245 Returns: 246 a dictionary containing the parameters' descriptions. 247 """ 248 options = [] 249 for param in self.options: 250 options.append(param.to_dict()) 251 252 values = [] 253 for param in self.values: 254 values.append(param.to_dict()) 255 256 return { 257 PARAM_KEY_OPTIONS: options, 258 PARAM_KEY_VALUES: values 259 } 260 261 def add_param(self, param: ConfigParam) -> None: 262 """Add a parameter to the internal dictionary. 263 264 This function is only used internally by the parameters, 265 use the other add functions instead. 266 Raises a KeyError when the name of the parameter is already present. 267 268 Args: 269 param: the parameter to add. 270 """ 271 if param.name in self.params: 272 raise KeyError('Config parameter already exists: ' + param.name) 273 274 self.params[param.name] = param 275 276 277def create_empty_parameters() -> ConfigParameters: 278 """Create the Config parameters with no entries.""" 279 return ConfigParameters() 280 281 282def create_params_random_seed() -> ConfigParameters: 283 """Create the configuration with only the random 'seed' parameter. 284 285 Returns: 286 the configuration parameters with one parameter: 'seed'. 287 """ 288 params = ConfigParameters() 289 params.add_random_seed(KEY_RANDOM_SEED) 290 return params
26class ConfigParameters: 27 """Config Parameters. 28 29 Container with varying Config parameters using a dictionary. 30 Moreover, he added option/value parameters are stored separately. 31 32 Public methods: 33 34 add_bool 35 add_multi_option 36 add_single_option 37 add_number 38 add_random_seed 39 add_range 40 get_defaults 41 get_num_params 42 get_param 43 get_param_names 44 to_dict 45 """ 46 47 def __init__(self): 48 """Construct the ConfigParameters.""" 49 self.params = {} 50 self.options = [] 51 self.values = [] 52 53 def add_bool(self, name: str, default_value: bool) -> None: 54 """Add a boolean parameter. 55 56 Sugar for an option parameter that is either True or False. 57 Raises a KeyError when the name of the parameter is already present. 58 59 Args: 60 name: name of the boolean parameter. 61 default_value: the default boolean value. 62 """ 63 param = create_bool_param(name, default_value) 64 65 self.add_param(param) 66 self.options.append(param) 67 68 def add_multi_option( 69 self, 70 name: str, 71 default_value: List[str], 72 options: List[str]) -> None: 73 """Add a multi option parameter. 74 75 The default_value and all the options are expected to be strings or None. 76 The default_value list is expected to have at least one entry. 77 The default_value list is expected to be present in the list of available options. 78 Raises a KeyError when the name of the parameter is already present. 79 80 Args: 81 name: the name of the parameter. 82 default_value: the default option(s) of the parameter. 83 options: list of available options for the parameter. 84 """ 85 param = ConfigMultiOptionParam( 86 name, 87 default_value, 88 options 89 ) 90 91 self.add_param(param) 92 self.options.append(param) 93 94 def add_single_option( 95 self, 96 name: str, 97 value_type: Type, 98 default_option: str, 99 options: List[str]) -> None: 100 """Add a single option parameter. 101 102 The default_value and all the options are expected to be strings. 103 The default_value is expected to be present in the list of available options. 104 The options list is expected to have unique values. 105 Raises a KeyError when the name of the parameter is already present. 106 107 Args: 108 name: the name of the parameter. 109 value_type: the type of the parameter. 110 default_option: default option of the parameter. 111 options: list of available options for the parameter. 112 """ 113 param = ConfigSingleOptionParam( 114 name, 115 value_type, 116 default_option, 117 options 118 ) 119 120 self.add_param(param) 121 self.options.append(param) 122 123 def add_number( 124 self, 125 name: str, 126 value_type: Type, 127 default_value: Union[int, float], 128 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 129 """Add a number parameter. 130 131 The value_type of default_value and min_max_value types are all expected to be either 132 int or float, conversions between the two during validation is available. 133 The default value is expected to be between the min_max_value. 134 The min_max_value is expected to have min_value <= max_value. 135 Raises a KeyError when the name of the parameter is already present. 136 137 Args: 138 name: the name of the parameter. 139 value_type: the type of the parameter. 140 default_value: the default value of the parameter. 141 min_max_value: tuple with the minimum and maximum value of the parameter. 142 """ 143 param = ConfigNumberParam( 144 name, 145 value_type, 146 default_value, 147 min_max_value 148 ) 149 150 self.add_param(param) 151 self.values.append(param) 152 153 def add_random_seed(self, name: str) -> None: 154 """Add a random seed parameter. 155 156 An integer number parameter, where the default_value is None. 157 Raises a KeyError when the name of the parameter is already present. 158 159 Args: 160 name: the name of the random seed parameter. 161 """ 162 param = ConfigRandomParam(name) 163 164 self.add_param(param) 165 self.values.append(param) 166 167 def add_range( 168 self, 169 name: str, 170 value_type: Type, 171 default_value: Union[Tuple[int, int], Tuple[float, float]], 172 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 173 """Add a range parameter. 174 175 The value_type of default_value and min_max_value types are all expected to be either 176 int or float, conversions between the two during validation is available. 177 The default_value min and max values are expected to be between the min_max_value. 178 The default_value and min_max_value are expected to have min_value <= max_value. 179 Raises a KeyError when the name of the parameter is already present. 180 181 Args: 182 name: the name of the parameter. 183 value_type: the type of the parameter. 184 default_value: tuple with the default minimum and maximum value of the parameter. 185 min_max_value: tuple with the minimum and maximum value of the parameter. 186 """ 187 param = ConfigRangeParam( 188 name, 189 value_type, 190 default_value, 191 min_max_value 192 ) 193 194 self.add_param(param) 195 self.values.append(param) 196 197 def get_defaults(self) -> Dict[str, Any]: 198 """Get the default values from all parameters. 199 200 Returns: 201 a dictionary containing name-default pairs for all parameters. 202 """ 203 defaults = {} 204 205 for param_name, param in self.params.items(): 206 defaults[param_name] = param.default_value 207 208 return defaults 209 210 def get_num_params(self) -> int: 211 """Get the number of parameters. 212 213 Returns: 214 the parameter count. 215 """ 216 return len(self.params) 217 218 def get_param(self, param_name: str) -> Optional[ConfigParam]: 219 """Get the parameter with the specified name. 220 221 Returns: 222 the parameter on success or None on failure. 223 """ 224 return self.params.get(param_name) 225 226 def get_param_names(self) -> List[str]: 227 """Get the names of all parameters. 228 229 Returns: 230 a list of all parameter names. 231 """ 232 param_names = [] 233 234 for param_name, _ in self.params.items(): 235 param_names.append(param_name) 236 237 return param_names 238 239 def to_dict(self) -> Dict[str, List[Dict[str, Any]]]: 240 """Get a dictionary describing all the parameters. 241 242 The parameters in the dictionary are stored separately: 243 PARAM_KEY_OPTIONS: list of all option parameter descriptions. 244 PARAM_KEY_VALUES: list of all value parameter descriptions. 245 246 Returns: 247 a dictionary containing the parameters' descriptions. 248 """ 249 options = [] 250 for param in self.options: 251 options.append(param.to_dict()) 252 253 values = [] 254 for param in self.values: 255 values.append(param.to_dict()) 256 257 return { 258 PARAM_KEY_OPTIONS: options, 259 PARAM_KEY_VALUES: values 260 } 261 262 def add_param(self, param: ConfigParam) -> None: 263 """Add a parameter to the internal dictionary. 264 265 This function is only used internally by the parameters, 266 use the other add functions instead. 267 Raises a KeyError when the name of the parameter is already present. 268 269 Args: 270 param: the parameter to add. 271 """ 272 if param.name in self.params: 273 raise KeyError('Config parameter already exists: ' + param.name) 274 275 self.params[param.name] = param
Config Parameters.
Container with varying Config parameters using a dictionary. Moreover, he added option/value parameters are stored separately.
Public methods:
add_bool add_multi_option add_single_option add_number add_random_seed add_range get_defaults get_num_params get_param get_param_names to_dict
47 def __init__(self): 48 """Construct the ConfigParameters.""" 49 self.params = {} 50 self.options = [] 51 self.values = []
Construct the ConfigParameters.
53 def add_bool(self, name: str, default_value: bool) -> None: 54 """Add a boolean parameter. 55 56 Sugar for an option parameter that is either True or False. 57 Raises a KeyError when the name of the parameter is already present. 58 59 Args: 60 name: name of the boolean parameter. 61 default_value: the default boolean value. 62 """ 63 param = create_bool_param(name, default_value) 64 65 self.add_param(param) 66 self.options.append(param)
Add a boolean parameter.
Sugar for an option parameter that is either True or False. Raises a KeyError when the name of the parameter is already present.
Args: name: name of the boolean parameter. default_value: the default boolean value.
68 def add_multi_option( 69 self, 70 name: str, 71 default_value: List[str], 72 options: List[str]) -> None: 73 """Add a multi option parameter. 74 75 The default_value and all the options are expected to be strings or None. 76 The default_value list is expected to have at least one entry. 77 The default_value list is expected to be present in the list of available options. 78 Raises a KeyError when the name of the parameter is already present. 79 80 Args: 81 name: the name of the parameter. 82 default_value: the default option(s) of the parameter. 83 options: list of available options for the parameter. 84 """ 85 param = ConfigMultiOptionParam( 86 name, 87 default_value, 88 options 89 ) 90 91 self.add_param(param) 92 self.options.append(param)
Add a 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. Raises a KeyError when the name of the parameter is already present.
Args: name: the name of the parameter. default_value: the default option(s) of the parameter. options: list of available options for the parameter.
94 def add_single_option( 95 self, 96 name: str, 97 value_type: Type, 98 default_option: str, 99 options: List[str]) -> None: 100 """Add a single option parameter. 101 102 The default_value and all the options are expected to be strings. 103 The default_value is expected to be present in the list of available options. 104 The options list is expected to have unique values. 105 Raises a KeyError when the name of the parameter is already present. 106 107 Args: 108 name: the name of the parameter. 109 value_type: the type of the parameter. 110 default_option: default option of the parameter. 111 options: list of available options for the parameter. 112 """ 113 param = ConfigSingleOptionParam( 114 name, 115 value_type, 116 default_option, 117 options 118 ) 119 120 self.add_param(param) 121 self.options.append(param)
Add a single option parameter.
The default_value and all the options are expected to be strings. The default_value is expected to be present in the list of available options. The options list is expected to have unique values. Raises a KeyError when the name of the parameter is already present.
Args: name: the name of the parameter. value_type: the type of the parameter. default_option: default option of the parameter. options: list of available options for the parameter.
123 def add_number( 124 self, 125 name: str, 126 value_type: Type, 127 default_value: Union[int, float], 128 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 129 """Add a number parameter. 130 131 The value_type of default_value and min_max_value types are all expected to be either 132 int or float, conversions between the two during validation is available. 133 The default value is expected to be between the min_max_value. 134 The min_max_value is expected to have min_value <= max_value. 135 Raises a KeyError when the name of the parameter is already present. 136 137 Args: 138 name: the name of the parameter. 139 value_type: the type of the parameter. 140 default_value: the default value of the parameter. 141 min_max_value: tuple with the minimum and maximum value of the parameter. 142 """ 143 param = ConfigNumberParam( 144 name, 145 value_type, 146 default_value, 147 min_max_value 148 ) 149 150 self.add_param(param) 151 self.values.append(param)
Add a 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. Raises a KeyError when the name of the parameter is already present.
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.
153 def add_random_seed(self, name: str) -> None: 154 """Add a random seed parameter. 155 156 An integer number parameter, where the default_value is None. 157 Raises a KeyError when the name of the parameter is already present. 158 159 Args: 160 name: the name of the random seed parameter. 161 """ 162 param = ConfigRandomParam(name) 163 164 self.add_param(param) 165 self.values.append(param)
Add a random seed parameter.
An integer number parameter, where the default_value is None. Raises a KeyError when the name of the parameter is already present.
Args: name: the name of the random seed parameter.
167 def add_range( 168 self, 169 name: str, 170 value_type: Type, 171 default_value: Union[Tuple[int, int], Tuple[float, float]], 172 min_max_value: Union[Tuple[int, int], Tuple[float, float]]) -> None: 173 """Add a range parameter. 174 175 The value_type of default_value and min_max_value types are all expected to be either 176 int or float, conversions between the two during validation is available. 177 The default_value min and max values are expected to be between the min_max_value. 178 The default_value and min_max_value are expected to have min_value <= max_value. 179 Raises a KeyError when the name of the parameter is already present. 180 181 Args: 182 name: the name of the parameter. 183 value_type: the type of the parameter. 184 default_value: tuple with the default minimum and maximum value of the parameter. 185 min_max_value: tuple with the minimum and maximum value of the parameter. 186 """ 187 param = ConfigRangeParam( 188 name, 189 value_type, 190 default_value, 191 min_max_value 192 ) 193 194 self.add_param(param) 195 self.values.append(param)
Add a 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. Raises a KeyError when the name of the parameter is already present.
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.
197 def get_defaults(self) -> Dict[str, Any]: 198 """Get the default values from all parameters. 199 200 Returns: 201 a dictionary containing name-default pairs for all parameters. 202 """ 203 defaults = {} 204 205 for param_name, param in self.params.items(): 206 defaults[param_name] = param.default_value 207 208 return defaults
Get the default values from all parameters.
Returns: a dictionary containing name-default pairs for all parameters.
210 def get_num_params(self) -> int: 211 """Get the number of parameters. 212 213 Returns: 214 the parameter count. 215 """ 216 return len(self.params)
Get the number of parameters.
Returns: the parameter count.
218 def get_param(self, param_name: str) -> Optional[ConfigParam]: 219 """Get the parameter with the specified name. 220 221 Returns: 222 the parameter on success or None on failure. 223 """ 224 return self.params.get(param_name)
Get the parameter with the specified name.
Returns: the parameter on success or None on failure.
226 def get_param_names(self) -> List[str]: 227 """Get the names of all parameters. 228 229 Returns: 230 a list of all parameter names. 231 """ 232 param_names = [] 233 234 for param_name, _ in self.params.items(): 235 param_names.append(param_name) 236 237 return param_names
Get the names of all parameters.
Returns: a list of all parameter names.
239 def to_dict(self) -> Dict[str, List[Dict[str, Any]]]: 240 """Get a dictionary describing all the parameters. 241 242 The parameters in the dictionary are stored separately: 243 PARAM_KEY_OPTIONS: list of all option parameter descriptions. 244 PARAM_KEY_VALUES: list of all value parameter descriptions. 245 246 Returns: 247 a dictionary containing the parameters' descriptions. 248 """ 249 options = [] 250 for param in self.options: 251 options.append(param.to_dict()) 252 253 values = [] 254 for param in self.values: 255 values.append(param.to_dict()) 256 257 return { 258 PARAM_KEY_OPTIONS: options, 259 PARAM_KEY_VALUES: values 260 }
Get a dictionary describing all the parameters.
The parameters in the dictionary are stored separately: PARAM_KEY_OPTIONS: list of all option parameter descriptions. PARAM_KEY_VALUES: list of all value parameter descriptions.
Returns: a dictionary containing the parameters' descriptions.
262 def add_param(self, param: ConfigParam) -> None: 263 """Add a parameter to the internal dictionary. 264 265 This function is only used internally by the parameters, 266 use the other add functions instead. 267 Raises a KeyError when the name of the parameter is already present. 268 269 Args: 270 param: the parameter to add. 271 """ 272 if param.name in self.params: 273 raise KeyError('Config parameter already exists: ' + param.name) 274 275 self.params[param.name] = param
Add a parameter to the internal dictionary.
This function is only used internally by the parameters, use the other add functions instead. Raises a KeyError when the name of the parameter is already present.
Args: param: the parameter to add.
278def create_empty_parameters() -> ConfigParameters: 279 """Create the Config parameters with no entries.""" 280 return ConfigParameters()
Create the Config parameters with no entries.
283def create_params_random_seed() -> ConfigParameters: 284 """Create the configuration with only the random 'seed' parameter. 285 286 Returns: 287 the configuration parameters with one parameter: 'seed'. 288 """ 289 params = ConfigParameters() 290 params.add_random_seed(KEY_RANDOM_SEED) 291 return params
Create the configuration with only the random 'seed' parameter.
Returns: the configuration parameters with one parameter: 'seed'.