| 272 | return p |
| 273 | |
| 274 | def set_value(self, new_value): |
| 275 | if len(self.valid_values) > 0 and new_value not in self.valid_values: |
| 276 | raise InvalidParameterError( |
| 277 | "Cannot set value '{0:s}' for parameter '{1:s}': not a valid value. Valid values are {2:s} ".format( |
| 278 | str(new_value), self.name, str(self.valid_values) |
| 279 | ) |
| 280 | ) |
| 281 | |
| 282 | if self.varType == NumberParameterType: |
| 283 | try: |
| 284 | # Sometimes a value must stay as an int for the script to work properly |
| 285 | if isinstance(new_value, int): |
| 286 | f = int(new_value) |
| 287 | else: |
| 288 | f = float(new_value) |
| 289 | |
| 290 | self.ast_node.value = f |
| 291 | except ValueError: |
| 292 | raise InvalidParameterError( |
| 293 | "Cannot set value '{0:s}' for parameter '{1:s}': parameter must be numeric.".format( |
| 294 | str(new_value), self.name |
| 295 | ) |
| 296 | ) |
| 297 | |
| 298 | elif self.varType == StringParameterType: |
| 299 | self.ast_node.value = str(new_value) |
| 300 | elif self.varType == BooleanParameterType: |
| 301 | if new_value: |
| 302 | self.ast_node.value = True |
| 303 | else: |
| 304 | self.ast_node.value = False |
| 305 | elif self.varType == TupleParameterType: |
| 306 | |
| 307 | # Build the list of constants to set as the tuple value |
| 308 | constants = [] |
| 309 | for nv in new_value: |
| 310 | constants.append(ast.Constant(value=nv)) |
| 311 | self.ast_node.elts = constants |
| 312 | ast.fix_missing_locations(self.ast_node) |
| 313 | else: |
| 314 | raise ValueError("Unknown Type of var: ", str(self.varType)) |
| 315 | |
| 316 | def __str__(self): |
| 317 | return "InputParameter: {name=%s, type=%s, defaultValue=%s" % ( |