Exemple #1
0
def add_commas(number, fixed_size=False):
    # Do not use unicode strings to fix python2 format bug. It doesn't work and crash.
    # See the correct fix at the end of function. 
    raw_with_commas = locale.format_string("%0.2f", number,
                                    grouping=True)
    locale_test = locale.format_string("%01.1f", 0.1) if not fixed_size else ''
    if len(locale_test) == 3 and not locale_test[1].isdigit():
        if locale_test[0] == locale.str(0) and locale_test[2] == locale.str(1):
            raw_with_commas = raw_with_commas.rstrip(locale_test[0]).rstrip(locale_test[1])
        elif locale_test[2] == locale.str(0) and locale_test[0] == locale.str(1):
            raw_with_commas = raw_with_commas.lstrip(locale_test[2]).lstrip(locale_test[1])

    # Fix python2 format bug: See https://bugs.python.org/issue15276
    # Note: This a crah in some platform, do not remove it because you can't reproduce it.
    try:
        return unicode(raw_with_commas)
    except UnicodeDecodeError:
        return raw_with_commas.decode("utf-8")
Exemple #2
0
 def remove_accents(text):
     from unicodedata import normalize, combining
     from singularity.code.pycompat import unicode
     nfkd_form = normalize('NFKD', unicode(text))
     return u"".join(c for c in nfkd_form if not combining(c))