diff options
Diffstat (limited to 'babel/numbers.py')
-rw-r--r-- | babel/numbers.py | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/babel/numbers.py b/babel/numbers.py index 5a05f91..399b70b 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -19,11 +19,11 @@ # - https://www.unicode.org/reports/tr35/ (Appendix G.6) from __future__ import annotations +import datetime import decimal import re -from typing import TYPE_CHECKING, Any, overload import warnings -import datetime +from typing import TYPE_CHECKING, Any, overload from babel.core import Locale, default_locale, get_global from babel.localedata import LocaleDataDict @@ -324,7 +324,7 @@ def get_decimal_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: :param locale: the `Locale` object or locale identifier """ - return Locale.parse(locale).number_symbols.get('decimal', u'.') + return Locale.parse(locale).number_symbols.get('decimal', '.') def get_plus_sign_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: @@ -335,7 +335,7 @@ def get_plus_sign_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: :param locale: the `Locale` object or locale identifier """ - return Locale.parse(locale).number_symbols.get('plusSign', u'+') + return Locale.parse(locale).number_symbols.get('plusSign', '+') def get_minus_sign_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: @@ -346,7 +346,7 @@ def get_minus_sign_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: :param locale: the `Locale` object or locale identifier """ - return Locale.parse(locale).number_symbols.get('minusSign', u'-') + return Locale.parse(locale).number_symbols.get('minusSign', '-') def get_exponential_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: @@ -357,7 +357,7 @@ def get_exponential_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: :param locale: the `Locale` object or locale identifier """ - return Locale.parse(locale).number_symbols.get('exponential', u'E') + return Locale.parse(locale).number_symbols.get('exponential', 'E') def get_group_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: @@ -368,11 +368,11 @@ def get_group_symbol(locale: Locale | str | None = LC_NUMERIC) -> str: :param locale: the `Locale` object or locale identifier """ - return Locale.parse(locale).number_symbols.get('group', u',') + return Locale.parse(locale).number_symbols.get('group', ',') def format_number(number: float | decimal.Decimal | str, locale: Locale | str | None = LC_NUMERIC) -> str: - u"""Return the given number formatted for a specific locale. + """Return the given number formatted for a specific locale. >>> format_number(1099, locale='en_US') # doctest: +SKIP u'1,099' @@ -418,7 +418,7 @@ def format_decimal( decimal_quantization: bool = True, group_separator: bool = True, ) -> str: - u"""Return the given decimal number formatted for a specific locale. + """Return the given decimal number formatted for a specific locale. >>> format_decimal(1.2345, locale='en_US') u'1.234' @@ -473,7 +473,7 @@ def format_compact_decimal( locale: Locale | str | None = LC_NUMERIC, fraction_digits: int = 0, ) -> str: - u"""Return the given decimal number formatted for a specific locale in compact form. + """Return the given decimal number formatted for a specific locale in compact form. >>> format_compact_decimal(12345, format_type="short", locale='en_US') u'12K' @@ -555,7 +555,7 @@ def format_currency( decimal_quantization: bool = True, group_separator: bool = True, ) -> str: - u"""Return formatted currency value. + """Return formatted currency value. >>> format_currency(1099.98, 'USD', locale='en_US') u'$1,099.98' @@ -653,7 +653,7 @@ def format_currency( try: pattern = locale.currency_formats[format_type] except KeyError: - raise UnknownCurrencyFormatError(f"{format_type!r} is not a known currency format type") + raise UnknownCurrencyFormatError(f"{format_type!r} is not a known currency format type") from None return pattern.apply( number, locale, currency=currency, currency_digits=currency_digits, @@ -711,7 +711,7 @@ def format_compact_currency( locale: Locale | str | None = LC_NUMERIC, fraction_digits: int = 0 ) -> str: - u"""Format a number as a currency value in compact form. + """Format a number as a currency value in compact form. >>> format_compact_currency(12345, 'USD', locale='en_US') u'$12K' @@ -870,8 +870,8 @@ def parse_number(string: str, locale: Locale | str | None = LC_NUMERIC) -> int: """ try: return int(string.replace(get_group_symbol(locale), '')) - except ValueError: - raise NumberFormatError(f"{string!r} is not a valid number") + except ValueError as ve: + raise NumberFormatError(f"{string!r} is not a valid number") from ve def parse_decimal(string: str, locale: Locale | str | None = LC_NUMERIC, strict: bool = False) -> decimal.Decimal: @@ -916,7 +916,7 @@ def parse_decimal(string: str, locale: Locale | str | None = LC_NUMERIC, strict: decimal_symbol = get_decimal_symbol(locale) if not strict and ( - group_symbol == u'\xa0' and # if the grouping symbol is U+00A0 NO-BREAK SPACE, + group_symbol == '\xa0' and # if the grouping symbol is U+00A0 NO-BREAK SPACE, group_symbol not in string and # and the string to be parsed does not contain it, ' ' in string # but it does contain a space instead, ): @@ -926,20 +926,20 @@ def parse_decimal(string: str, locale: Locale | str | None = LC_NUMERIC, strict: try: parsed = decimal.Decimal(string.replace(group_symbol, '') .replace(decimal_symbol, '.')) - except decimal.InvalidOperation: - raise NumberFormatError(f"{string!r} is not a valid decimal number") + except decimal.InvalidOperation as exc: + raise NumberFormatError(f"{string!r} is not a valid decimal number") from exc if strict and group_symbol in string: proper = format_decimal(parsed, locale=locale, decimal_quantization=False) if string != proper and string.rstrip('0') != (proper + decimal_symbol): try: parsed_alt = decimal.Decimal(string.replace(decimal_symbol, '') .replace(group_symbol, '.')) - except decimal.InvalidOperation: + except decimal.InvalidOperation as exc: raise NumberFormatError( f"{string!r} is not a properly formatted decimal number. " f"Did you mean {proper!r}?", suggestions=[proper], - ) + ) from exc else: proper_alt = format_decimal(parsed_alt, locale=locale, decimal_quantization=False) if proper_alt == proper: @@ -1095,7 +1095,7 @@ class NumberPattern: scale = 0 if '%' in ''.join(self.prefix + self.suffix): scale = 2 - elif u'‰' in ''.join(self.prefix + self.suffix): + elif '‰' in ''.join(self.prefix + self.suffix): scale = 3 return scale @@ -1222,11 +1222,11 @@ class NumberPattern: number if self.number_pattern != '' else '', self.suffix[is_negative]]) - if u'¤' in retval: - retval = retval.replace(u'¤¤¤', + if '¤' in retval: + retval = retval.replace('¤¤¤', get_currency_name(currency, value, locale)) - retval = retval.replace(u'¤¤', currency.upper()) - retval = retval.replace(u'¤', get_currency_symbol(currency, locale)) + retval = retval.replace('¤¤', currency.upper()) + retval = retval.replace('¤', get_currency_symbol(currency, locale)) # remove single quotes around text, except for doubled single quotes # which are replaced with a single quote |