def is_larger(unit_1, unit_2): """Returns a boolean indicating whether unit_1 is larger than unit_2. E.g: >>> is_larger('KB', 'B') True >>> is_larger('min', 'day') False """ unit_1 = functions.value_for_key(INFORMATION_UNITS, unit_1) unit_2 = functions.value_for_key(INFORMATION_UNITS, unit_2) return ureg.parse_expression(unit_1) > ureg.parse_expression(unit_2)
def convert(value, source_unit, target_unit, fmt=False): """Converts value from source_unit to target_unit. Returns a tuple containing the converted value and target_unit. Having fmt set to True causes the value to be formatted to 1 decimal digit if it's a decimal or be formatted as integer if it's an integer. E.g: >>> convert(2, 'hr', 'min') (120.0, 'min') >>> convert(2, 'hr', 'min', fmt=True) (120, 'min') >>> convert(30, 'min', 'hr', fmt=True) (0.5, 'hr') """ orig_target_unit = target_unit source_unit = functions.value_for_key(INFORMATION_UNITS, source_unit) target_unit = functions.value_for_key(INFORMATION_UNITS, target_unit) q = ureg.Quantity(value, source_unit) q = q.to(ureg.parse_expression(target_unit)) value = functions.format_value(q.magnitude) if fmt else q.magnitude return value, orig_target_unit