Esempio n. 1
0
def _normalize_information(value, unit):
    value = decimal.Decimal(str(value))

    while value < 1:
        prev_unit = functions.previous_key(INFORMATION_UNITS, unit)
        if prev_unit is None:
            break
        value, unit = convert(value, unit, prev_unit)

    while value >= 1024:
        next_unit = functions.next_key(INFORMATION_UNITS, unit)
        if next_unit is None:
            break
        value, unit = convert(value, unit, next_unit)

    return functions.format_value(value), unit
Esempio n. 2
0
def _normalize_information(value, unit):
    value = decimal.Decimal(str(value))

    while value < 1:
        prev_unit = functions.previous_key(INFORMATION_UNITS, unit)
        if prev_unit is None:
            break
        value, unit = convert(value, unit, prev_unit)

    while value >= 1024:
        next_unit = functions.next_key(INFORMATION_UNITS, unit)
        if next_unit is None:
            break
        value, unit = convert(value, unit, next_unit)

    return functions.format_value(value), unit
Esempio n. 3
0
def _normalize_time(value, unit):
    value, unit = convert(value, unit, 's')

    if value >= 60:
        value, unit = convert(value, 's', 'min')

        if value >= 60:
            value, unit = convert(value, 'min', 'hr')

            if value >= 24:
                value, unit = convert(value, 'hr', 'day')

                if value >= 365:
                    value, unit = convert(value, 'day', 'year')
                elif value >= 31:
                    value, unit = convert(value, 'day', 'month')
                elif value >= 7:
                    value, unit = convert(value, 'day', 'week')

    return functions.format_value(value), unit
Esempio n. 4
0
def _normalize_time(value, unit):
    # Normalize time by converting to next higher unit when value is
    # at least 2 units
    value, unit = convert(value, unit, 's')

    if value >= 120:
        value, unit = convert(value, 's', 'min')

        if value >= 120:
            value, unit = convert(value, 'min', 'hr')

            if value >= 48:
                value, unit = convert(value, 'hr', 'day')

                if value >= 730:
                    value, unit = convert(value, 'day', 'year')
                elif value >= 62:
                    value, unit = convert(value, 'day', 'month')
                elif value >= 14:
                    value, unit = convert(value, 'day', 'week')

    return functions.format_value(value), unit
Esempio n. 5
0
def _normalize_time(value, unit):
    # Normalize time by converting to next higher unit when value is
    # at least 2 units
    value, unit = convert(value, unit, "s")

    if value >= 120:
        value, unit = convert(value, "s", "min")

        if value >= 120:
            value, unit = convert(value, "min", "hr")

            if value >= 48:
                value, unit = convert(value, "hr", "day")

                if value >= 730:
                    value, unit = convert(value, "day", "year")
                elif value >= 62:
                    value, unit = convert(value, "day", "month")
                elif value >= 14:
                    value, unit = convert(value, "day", "week")

    return functions.format_value(value), unit
Esempio n. 6
0
def _normalize_time(value, unit):
    # Normalize time by converting to next higher unit when value is
    # at least 2 units
    value, unit = convert(value, unit, 's')

    if value >= 120:
        value, unit = convert(value, 's', 'min')

        if value >= 120:
            value, unit = convert(value, 'min', 'hr')

            if value >= 48:
                value, unit = convert(value, 'hr', 'day')

                if value >= 730:
                    value, unit = convert(value, 'day', 'year')
                elif value >= 62:
                    value, unit = convert(value, 'day', 'month')
                elif value >= 14:
                    value, unit = convert(value, 'day', 'week')

    return functions.format_value(value), unit
Esempio n. 7
0
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
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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