diff options
author | Isaac Jurado <diptongo@gmail.com> | 2016-05-29 09:40:24 +0200 |
---|---|---|
committer | Isaac Jurado <diptongo@gmail.com> | 2016-05-29 09:40:24 +0200 |
commit | 8341064c151b50135ac083b33117420efeda4d96 (patch) | |
tree | 63e9c0d59e3b0247e639a3bf59e3c3a5872c1113 /babel | |
parent | 6238f6620c54d0343e923ef92130b35f2bd117d0 (diff) | |
download | babel-8341064c151b50135ac083b33117420efeda4d96.tar.gz |
Simplify the selection of decimal implementation
Instead of selecting individual symbols, expose only the chosen decimal module
through babel._compat. This forces Babel to always use "decimal." prefix.
Howver, it will simplify the code for users that need to manipulate the decimal
context.
Diffstat (limited to 'babel')
-rw-r--r-- | babel/_compat.py | 20 | ||||
-rw-r--r-- | babel/numbers.py | 16 | ||||
-rw-r--r-- | babel/plural.py | 6 | ||||
-rw-r--r-- | babel/units.py | 4 |
4 files changed, 22 insertions, 24 deletions
diff --git a/babel/_compat.py b/babel/_compat.py index ae290f4..aea3389 100644 --- a/babel/_compat.py +++ b/babel/_compat.py @@ -58,15 +58,13 @@ number_types = integer_types + (float,) # -# Use cdecimal when available +# Since Python 3.3, a fast decimal implementation is already included in the +# standard library. Otherwise use cdecimal when available # -from decimal import (Decimal as _dec, - InvalidOperation as _invop) -try: - from cdecimal import (Decimal as _cdec, - InvalidOperation as _cinvop) - Decimal = _cdec - InvalidOperation = (_invop, _cinvop) -except ImportError: - Decimal = _dec - InvalidOperation = _invop +if sys.version_info[:2] >= (3, 3): + import decimal +else: + try: + import cdecimal as decimal + except ImportError: + import decimal diff --git a/babel/numbers.py b/babel/numbers.py index 67881ae..f9be88d 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -22,7 +22,7 @@ import re from datetime import date as date_, datetime as datetime_ from babel.core import default_locale, Locale, get_global -from babel._compat import Decimal, InvalidOperation +from babel._compat import decimal LC_NUMERIC = default_locale('LC_NUMERIC') @@ -437,9 +437,9 @@ def parse_decimal(string, locale=LC_NUMERIC): """ locale = Locale.parse(locale) try: - return Decimal(string.replace(get_group_symbol(locale), '') - .replace(get_decimal_symbol(locale), '.')) - except InvalidOperation: + return decimal.Decimal(string.replace(get_group_symbol(locale), '') + .replace(get_decimal_symbol(locale), '.')) + except decimal.InvalidOperation: raise NumberFormatError('%r is not a valid decimal number' % string) @@ -566,8 +566,8 @@ class NumberPattern(object): def apply(self, value, locale, currency=None, force_frac=None): frac_prec = force_frac or self.frac_prec - if not isinstance(value, Decimal): - value = Decimal(str(value)) + if not isinstance(value, decimal.Decimal): + value = decimal.Decimal(str(value)) value = value.scaleb(self.scale) is_negative = int(value.is_signed()) if self.exp_prec: # Scientific notation @@ -603,7 +603,7 @@ class NumberPattern(object): if sep: number += get_decimal_symbol(locale) + b else: # A normal number pattern - precision = Decimal('1.' + '1' * frac_prec[1]) + precision = decimal.Decimal('1.' + '1' * frac_prec[1]) rounded = value.quantize(precision) a, sep, b = str(abs(rounded)).partition(".") number = (self._format_int(a, self.int_prec[0], @@ -641,7 +641,7 @@ class NumberPattern(object): def _format_significant(self, value, minimum, maximum): exp = value.adjusted() scale = maximum - 1 - exp - digits = str(value.scaleb(scale).quantize(Decimal(1))) + digits = str(value.scaleb(scale).quantize(decimal.Decimal(1))) if scale <= 0: result = digits + '0' * -scale else: diff --git a/babel/plural.py b/babel/plural.py index 980629d..0b8e425 100644 --- a/babel/plural.py +++ b/babel/plural.py @@ -11,7 +11,7 @@ import re import sys -from babel._compat import Decimal +from babel._compat import decimal _plural_tags = ('zero', 'one', 'two', 'few', 'many', 'other') @@ -33,9 +33,9 @@ def extract_operands(source): # 2.6's Decimal cannot convert from float directly if sys.version_info < (2, 7): n = str(n) - n = Decimal(n) + n = decimal.Decimal(n) - if isinstance(n, Decimal): + if isinstance(n, decimal.Decimal): dec_tuple = n.as_tuple() exp = dec_tuple.exponent fraction_digits = dec_tuple.digits[exp:] if exp < 0 else () diff --git a/babel/units.py b/babel/units.py index 798ade2..1ea5b17 100644 --- a/babel/units.py +++ b/babel/units.py @@ -80,8 +80,8 @@ def format_unit(value, measurement_unit, length='long', format=None, locale=LC_N Number formats may be overridden with the ``format`` parameter. - >>> from babel._compat import Decimal - >>> format_unit(Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr') + >>> from babel._compat import decimal + >>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr') u'-42,8 \\xb0C' The locale's usual pluralization rules are respected. |