def validate_scalar_unit(self): regex = re.compile(r'([0-9.]+)\s*(\w+)') try: result = regex.match(str(self.value)).groups() validateutils.str_to_num(result[0]) scalar_unit = self._check_unit_in_scalar_standard_units(result[1]) self.value = ' '.join([result[0], scalar_unit]) return self.value except Exception: ExceptionCollector.appendException( ValueError(_('"%s" is not a valid scalar-unit.') % self.value))
def validate_scalar_unit(self): regex = re.compile('([0-9.]+)\s*(\w+)') try: result = regex.match(str(self.value)).groups() validateutils.str_to_num(result[0]) scalar_unit = self._check_unit_in_scalar_standard_units(result[1]) self.value = ' '.join([result[0], scalar_unit]) return self.value except Exception: raise ValueError(_('"%s" is not a valid scalar-unit') % self.value)
def get_num_from_scalar_unit(self, unit=None): if unit: unit = self._check_unit_in_scalar_standard_units(unit) else: unit = self.SCALAR_UNIT_DEFAULT self.validate_scalar_unit() regex = re.compile('([0-9.]+)\s*(\w+)') result = regex.match(str(self.value)).groups() converted = (float(validateutils.str_to_num(result[0])) * self.SCALAR_UNIT_DICT[result[1]] / self.SCALAR_UNIT_DICT[unit]) if converted - int(converted) < 0.0000000000001: converted = int(converted) return converted