Exemplo n.º 1
0
    def format_babel(self, locale):
        """translate the name of the system

        :type locale: Locale
        """
        if locale and self.name in _babel_systems:
            name = _babel_systems[self.name]
            locale = Loc.parse(locale)
            return locale.measurement_systems[name]
        return self.name
Exemplo n.º 2
0
 def format_babel(self, locale):
     """translate the name of the system
     
     :type locale: Locale
     """
     if locale and self.name in _babel_systems:
         name = _babel_systems[self.name]
         locale = Loc.parse(locale)
         return locale.measurement_systems[name]
     return self.name
Exemplo n.º 3
0
def formatter(items,
              as_ratio=True,
              single_denominator=False,
              product_fmt=' * ',
              division_fmt=' / ',
              power_fmt='{} ** {}',
              parentheses_fmt='({0})',
              exp_call=lambda x: '{0:n}'.format(x),
              locale=None,
              babel_length='long',
              babel_plural_form='one'):
    """Format a list of (name, exponent) pairs.

    :param items: a list of (name, exponent) pairs.
    :param as_ratio: True to display as ratio, False as negative powers.
    :param single_denominator: all with terms with negative exponents are
                               collected together.
    :param product_fmt: the format used for multiplication.
    :param division_fmt: the format used for division.
    :param power_fmt: the format used for exponentiation.
    :param parentheses_fmt: the format used for parenthesis.
    :param locale: the locale object as defined in babel.
    :param babel_length: the length of the translated unit, as defined in babel cldr.
    :param babel_plural_form: the plural form, calculated as defined in babel.

    :return: the formula as a string.
    """

    if not items:
        return ''

    if as_ratio:
        fun = lambda x: exp_call(abs(x))
    else:
        fun = exp_call

    pos_terms, neg_terms = [], []

    for key, value in sorted(items):
        if locale and babel_length and babel_plural_form and key in _babel_units:
            _key = _babel_units[key]
            locale = Loc.parse(locale)
            unit_patterns = locale._data['unit_patterns']
            compound_unit_patterns = locale._data["compound_unit_patterns"]
            plural = 'one' if abs(value) <= 0 else babel_plural_form
            if babel_length not in _babel_lengths:
                other_lengths = [
                    _babel_length for _babel_length in reversed(_babel_lengths) \
                    if babel_length != _babel_length
                ]
            else:
                other_lengths = []
            for _babel_length in [babel_length] + other_lengths:
                pat = unit_patterns.get(_key, {}).get(_babel_length,
                                                      {}).get(plural)
                if pat is not None:
                    # Don't remove this positional! This is the format used in Babel
                    key = pat.replace('{0}', '').strip()
                    break
            division_fmt = compound_unit_patterns.get("per", {}).get(
                babel_length, division_fmt)
            power_fmt = '{}{}'
            exp_call = _pretty_fmt_exponent
        if value == 1:
            pos_terms.append(key)
        elif value > 0:
            pos_terms.append(power_fmt.format(key, fun(value)))
        elif value == -1 and as_ratio:
            neg_terms.append(key)
        else:
            neg_terms.append(power_fmt.format(key, fun(value)))

    if not as_ratio:
        # Show as Product: positive * negative terms ** -1
        return _join(product_fmt, pos_terms + neg_terms)

    # Show as Ratio: positive terms / negative terms
    pos_ret = _join(product_fmt, pos_terms) or '1'

    if not neg_terms:
        return pos_ret

    if single_denominator:
        neg_ret = _join(product_fmt, neg_terms)
        if len(neg_terms) > 1:
            neg_ret = parentheses_fmt.format(neg_ret)
    else:
        neg_ret = _join(division_fmt, neg_terms)

    return _join(division_fmt, [pos_ret, neg_ret])
Exemplo n.º 4
0
def formatter(items, as_ratio=True, single_denominator=False,
              product_fmt=' * ', division_fmt=' / ', power_fmt='{0} ** {1}',
              parentheses_fmt='({0})', exp_call=lambda x: '{0:n}'.format(x),
              locale=None, babel_length='long', babel_plural_form='one'):
    """Format a list of (name, exponent) pairs.

    :param items: a list of (name, exponent) pairs.
    :param as_ratio: True to display as ratio, False as negative powers.
    :param single_denominator: all with terms with negative exponents are
                               collected together.
    :param product_fmt: the format used for multiplication.
    :param division_fmt: the format used for division.
    :param power_fmt: the format used for exponentiation.
    :param parentheses_fmt: the format used for parenthesis.
    :param locale: the locale object as defined in babel.
    :param babel_length: the length of the translated unit, as defined in babel cldr.
    :param babel_plural_form: the plural form, calculated as defined in babel.

    :return: the formula as a string.
    """

    if not items:
        return ''

    if as_ratio:
        fun = lambda x: exp_call(abs(x))
    else:
        fun = exp_call

    pos_terms, neg_terms = [], []

    for key, value in sorted(items):
        if locale and babel_length and babel_plural_form and key in _babel_units:
            _key = _babel_units[key]
            locale = Loc.parse(locale)
            unit_patterns = locale._data['unit_patterns']
            compound_unit_patterns = locale._data["compound_unit_patterns"]
            plural = 'one' if abs(value) <= 0 else babel_plural_form
            if babel_length not in _babel_lengths:
                other_lengths = [
                    _babel_length for _babel_length in reversed(_babel_lengths) \
                    if babel_length != _babel_length
                ]
            else:
                other_lengths = []
            for _babel_length in [babel_length] + other_lengths:
                pat = unit_patterns.get(_key, {}).get(_babel_length, {}).get(plural)
                print(plural, _babel_length, pat)
                if pat is not None:
                    key = pat.replace('{0}', '').strip()
                    break
            division_fmt = compound_unit_patterns.get("per", {}).get(babel_length, division_fmt)
            power_fmt = '{0}{1}'
            exp_call = _pretty_fmt_exponent
        if value == 1:
            pos_terms.append(key)
        elif value > 0:
            pos_terms.append(power_fmt.format(key, fun(value)))
        elif value == -1 and as_ratio:
            neg_terms.append(key)
        else:
            neg_terms.append(power_fmt.format(key, fun(value)))

    if not as_ratio:
        # Show as Product: positive * negative terms ** -1
        return _join(product_fmt, pos_terms + neg_terms)

    # Show as Ratio: positive terms / negative terms
    pos_ret = _join(product_fmt, pos_terms) or '1'

    if not neg_terms:
        return pos_ret

    if single_denominator:
        neg_ret = _join(product_fmt, neg_terms)
        if len(neg_terms) > 1:
            neg_ret = parentheses_fmt.format(neg_ret)
    else:
        neg_ret = _join(division_fmt, neg_terms)

    return _join(division_fmt, [pos_ret, neg_ret])