def check_otf_map(value): if not isinstance(value, list): raise v.ValidateError("expected list, found %s" % (value,)) _frame = check_frame(value[0]) scan_axis = value[1].upper() logger.debug("scan axis: %s" % (scan_axis,)) if not scan_axis in frame.axes: raise v.ValidateError("not a valid axis: %s" % (scan_axis,)) start_point = value[2].upper() if not start_point in START_POINTS: raise v.ValidateError("not a valid start point: %s" % (start_point,)) length_x = angle_parser.check_angle(value[3]) length_y = angle_parser.check_angle(value[4]) speed = v.is_float(value[5], min=0) try: spacing = angle_parser.check_angle(value[6]) except: logger.debug("OTF map specify scans per beam") spacing = v.is_float(value[6], min=1) if scan_axis == "BOTH": logger.debug("exploding into separate scans") return (OTFMapScan(_frame, start_point, _frame.lon_name, length_x, length_y, spacing, speed), OTFMapScan(_frame, start_point, _frame.lat_name, length_x, length_y, spacing, speed)) else: logger.debug("got otf map") return OTFMapScan(_frame, start_point, scan_axis, length_x, length_y, spacing, speed)
def validate(self, latitude, longitude): try: lat = is_float(latitude, min=ConstraintAttributes.MIN_LAT, max=ConstraintAttributes.MAX_LAT) except VdtTypeError: raise LatitudeNotFloat(latitude) except VdtValueError: raise LatitudeNotInRange(latitude) try: long = is_float(longitude, min=ConstraintAttributes.MIN_LONG, max=ConstraintAttributes.MAX_LONG) except VdtTypeError: raise LongitudeNotFloat(longitude) except VdtValueError: raise LongitudeNotInRange(longitude) return lat, long
def _is_float_list_or_none(self, value, min=None, max=None): """ Validation function for a list of float values or None. Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py """ # Check if value is string if isinstance(value, str): # Convert string value try: value = ast.literal_eval(value) except ValueError: raise ValidateError( 'The value "{}" could not be converted!'.format(value)) except Exception as e: raise e if isinstance(value, list): return [is_float(elem) for elem in is_list(value, min, max)] elif value is None: return value else: raise ValidateError( 'A value of type {} was passed when a float list or None type was expected!' .format(type(value)))
def check_cross_scan(value): if not isinstance(value, list): raise v.ValidateError("expected list, found %s" % (value,)) _frame = check_frame(value[0]) length = angle_parser.check_angle(value[1]) speed = v.is_float(value[2]) return CrossScan(_frame, length, speed)
def _get_telephone_number(self, number_as_given): try: number_as_given = self._strip_decimals(is_float(number_as_given)) except Exception: pass if number_as_given is not None: return "".join([num for num in number_as_given if num.isdigit()]) return number_as_given
def check_nodding(value): if not isinstance(value, list): raise v.ValidateError("expected list, found %s" % (value,)) feed_a = v.is_integer(value[0], min=0) feed_b = v.is_integer(value[1], min=0) duration = v.is_float(value[2], min=0) sequence = check_nodding_sequence(value[3][1:-1]) #strip [ and ] return NoddingScan((feed_a, feed_b), duration, sequence)
def check_onoff(value): if not isinstance(value, list): raise v.ValidateError("expected list, found %s" % (value,)) duration = v.is_float(value[0], min=0) offset_frame = check_frame(value[1]) offset_lon = angle_parser.check_angle(value[2]) offset_lat = angle_parser.check_angle(value[3]) sequence = check_onoff_sequence(value[4][1:-1]) #strip [ and ] return OnOffScan(duration, offset_lon, offset_lat, offset_frame, sequence)
def _is_float_or_none(self, value, min=None, max=None): """ Validation function for an float values or None. Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py """ if value in set((None, 'none', 'None')): return None else: return is_float(value, min=min, max=max)
def check_raster_map(value): if not isinstance(value, list): raise v.ValidateError("expected list, found %s" % (value,)) _frame = check_frame(value[0]) scan_axis = value[1].upper() if not scan_axis in frame.axes: raise v.ValidateError("not a valid axis: %s" % (scan_axis,)) start_point = value[2].upper() if not start_point in START_POINTS: raise v.ValidateError("not a valid start point: %s" % (start_point,)) length_x = angle_parser.check_angle(value[3]) length_y = angle_parser.check_angle(value[4]) duration = v.is_float(value[5], min=0) try: spacing = angle_parser.check_angle(value[6]) except: logger.debug("RASTER map specify scans per beam") spacing = v.is_float(value[6], min=1) return RasterMapScan(_frame, start_point, scan_axis, length_x, length_y, spacing, duration)
def real_scalar_or_real_numpy_array_check( value: Union[str, List[str]], min: Optional[float] = None, max: Optional[float] = None) -> Union[float, List[float]]: """ Parse and validate `value` as a float number if possible and, if not, parse it 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. The difference regarding the `real_numpy_array_check` function is that if value is a single number it will be parsed as a single float value, instead of being parsed as a real numpy array with a single element. Parameters ---------- value : str | list[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 : float The minimum allowed value. If the converted value is (or have) lower than `min` then the VdtValueTooSmallError exception will be raised. max : float The maximum allowed value. If the converted value is (or have) greater than `man` then the VdtValueTooSmallError exception will be raised. Returns ------- float | List[float] 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] """ try: return cast(float, validate.is_float(value, min, max)) except validate.VdtTypeError: return real_numpy_array_check(value, min, max)
def real_scalar_or_real_numpy_array_check(value, min=None, max=None): """ Parse and validate `value` as a floar number if possible and, if not, parse it 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. The difference regarding the `real_numpy_array_check` function is that if value is a single number it will be parsed as a single float value, instead of being parsed as a real numpy array with a single element. 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 ------- numpy array 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. .. code:: SNR = 0,5,10:20 SNR = [0 5 10:20] """ try: value = validate.is_float(value, min, max) except validate.VdtTypeError: value = real_numpy_array_check(value, min, max) return value
def any_checker(value): ''' Convert value to its built-in data type if possible Convert a string value to its built-in data type (integer, float, boolean, str or list of these) if possible Args: value (:obj:`object`): a value to be converted Returns: :obj:`type`: the converted value Raises: :obj:`VdtTypeError`: if the value cannot be converted ''' if not isinstance(value, float) or not math.isnan(value): # if statement needed because `_handle_value` doesn't seem to be able to handle nan value, _ = ConfigObj()._handle_value(value) # parse to integer try: return is_integer(value) except VdtTypeError: pass # parse to float try: return is_float(value) except VdtTypeError: pass # parse to bool try: return is_boolean(value) except VdtTypeError: pass # parse to list try: return [any_checker(val) for val in is_list(value)] except VdtTypeError: pass # parse to string return is_string(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
def validate(self, value): return is_float(value, min=self.min, max=self.max)
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 ------- numpy array 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. .. code:: 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 any commas by a space 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