def is_sci_integer(value, min=None, max=None): """Function for the validate module to support scientific notation""" value = validate_convert(value, sci_int) minval = validate_convert(min, sci_int, "min") maxval = validate_convert(max, sci_int, "max") if min is not None and value < minval: raise validate.VdtValueTooSmallError(value) if max is not None and value > maxval: raise validate.VdtValueTooBigError(value) return value
def real_numpy_array_check(value, min=None, max=None): """ Parse and validate `value` as a numpy array (of floats). Value can be either a single number, a range expression in the form of min:max or min:step:max, or even a list containing numbers and range expressions. Parameters ---------- value : str The string to be converted. This can be either a single number, a range expression in the form of min:max or min:step:max, or even a list containing numbers and range expressions. min : int The minimum allowed value. If the converted value is (or have) lower than `min` then the VdtValueTooSmallError exception will be raised. max : int The maximum allowed value. If the converted value is (or have) greater than `man` then the VdtValueTooSmallError exception will be raised. Returns ------- np.ndarray The parsed numpy array. Notes ----- You can either separate the values with commas or spaces (any comma will have the same effect as a space). However, if you separate with spaces the values should be in brackets, while if you separate with commands there should be no brackets. >> SNR = 0,5,10:20 >> SNR = [0 5 10:20] """ if isinstance(value, str): # Remove '[' and ']' if they exist. if value[0] == '[' and value[-1] == ']': value = value[1:-1].strip() value = value.replace(',', ' ') # Replace commas with spaces value = value.split() # Split based on spaces # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Test if it is a list or not if isinstance(value, list): # If it is a list, each element can be either a number of a 'range # expression' that can be parsed with _parse_float_range_expr. We # simple apply real_numpy_array_check on each element in the list # to do the work and stack horizontally all the results. value = [real_numpy_array_check(a, min, max) for a in value] out = np.hstack(value) else: # It its not a list, it can be either a single number of a 'range # expression' that can be parsed with _parse_float_range_expr try: value = validate.is_float(value) out = np.array([value]) except validate.VdtTypeError: out = _parse_float_range_expr(value) # xxxxxxxxxx Validate if minimum and maximum allowed values xxxxxxxxxxx if min is not None: # maybe "min" was passed as a string and thus we need to convert it # to a float min = float(min) if out.min() < min: raise validate.VdtValueTooSmallError(out.min()) if max is not None: # maybe "min" was passed as a string and thus we need to convert it # to a float max = float(max) if out.max() > max: raise validate.VdtValueTooBigError(out.max()) # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx return out