def to_int(param, param_name): """ Transfer param to int type. Args: param (Any): A param transformed. param_name (str): Param name. Returns: int, value after transformed. """ try: param = int(param) except ValueError: raise exceptions.ParamTypeError(param_name, 'Integer') return param
def if_nan_inf_to_none(name, value): """ Transform value to None if it is NaN or Inf. Args: name (str): Name of value. value (float): A number transformed. Returns: float, if value is NaN or Inf, return None. """ if not isinstance(value, Number): raise exceptions.ParamTypeError(name, 'number') if math.isnan(value) or math.isinf(value): value = None return value
def to_float(param, param_name): """ Transfer param to float type. Args: param (Any): A param transformed. param_name (str): Param name. Returns: float, value after transformed. """ try: param = float(param) except ValueError: raise exceptions.ParamTypeError(param_name, 'Float') return param
def str_to_bool(param, param_name): """ Check param and transform it to bool. Args: param (str): 'true' or 'false' is valid. param_name (str): Param name. Returns: bool, if param is 'true', case insensitive. Raises: ParamValueError: If the value of param is not 'false' and 'true'. """ if not isinstance(param, str): raise exceptions.ParamTypeError(param_name, 'str') if param.lower() not in ['false', 'true']: raise exceptions.ParamValueError("The value of %s must be 'false' or 'true'." % param_name) param = (param.lower() == 'true') return param