def normalize(value, unit): """Converts the value so that it belongs to some expected range. Returns the new value and new unit. E.g: >>> normalize(1024, 'KB') (1, 'MB') >>> normalize(90, 'min') (1.5, 'hr') >>> normalize(1.0, 'object') (1, 'object') """ if value < 0: raise ValueError('Negative value: %s %s.' % (value, unit)) if unit in functions.get_keys(INFORMATION_UNITS): return _normalize_information(value, unit) elif unit in TIME_UNITS: return _normalize_time(value, unit) else: # Unknown unit, just return it return functions.format_value(value), unit
def normalize(value, unit): """Converts the value so that it belongs to some expected range. Returns the new value and new unit. E.g: >>> normalize(1024, 'KB') (1, 'MB') >>> normalize(90, 'min') (1.5, 'hr') >>> normalize(1.0, 'object') (1, 'object') """ if value < 0: raise ValueError("Negative value: %s %s." % (value, unit)) if unit in functions.get_keys(INFORMATION_UNITS): return _normalize_information(value, unit) elif unit in TIME_UNITS: return _normalize_time(value, unit) else: # Unknown unit, just return it return functions.format_value(value), unit
def is_supported(unit): """Returns a bool indicating whether the unit specified is supported by this module. """ return unit in functions.get_keys(INFORMATION_UNITS) + TIME_UNITS