diff options
author | Gaëtan de Menten <gdementen@gmail.com> | 2010-02-28 20:39:49 +0000 |
---|---|---|
committer | Gaëtan de Menten <gdementen@gmail.com> | 2010-02-28 20:39:49 +0000 |
commit | 08f2c2c19a2f66eaf3c243bd1db7ace82f0e1286 (patch) | |
tree | 966b75b2d875cc6722751d1d4df48e3696c2ebec /lib/sqlalchemy/processors.py | |
parent | 9fd094edbb4d7f160fcb36ae96f39514ac1d1f88 (diff) | |
download | sqlalchemy-08f2c2c19a2f66eaf3c243bd1db7ace82f0e1286.tar.gz |
support scale argument for the C implementation of the decimal processor
Diffstat (limited to 'lib/sqlalchemy/processors.py')
-rw-r--r-- | lib/sqlalchemy/processors.py | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index 04fa5054a..c99ca4c6f 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -4,7 +4,8 @@ # This module is part of SQLAlchemy and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -"""defines generic type conversion functions, as used in result processors. +"""defines generic type conversion functions, as used in bind and result +processors. They all share one common characteristic: None is passed through unchanged. @@ -39,9 +40,13 @@ try: else: return UnicodeResultProcessor(encoding).process - # TODO: add scale argument - #def to_decimal_processor_factory(target_class): - # return DecimalResultProcessor(target_class).process + def to_decimal_processor_factory(target_class, scale=10): + # Note that the scale argument is not taken into account for integer + # values in the C implementation while it is in the Python one. + # For example, the Python implementation might return + # Decimal('5.00000') whereas the C implementation will + # return Decimal('5'). These are equivalent of course. + return DecimalResultProcessor(target_class, "%%.%df" % scale).process except ImportError: def to_unicode_processor_factory(encoding, errors=None): @@ -54,18 +59,18 @@ except ImportError: # decoder returns a tuple: (value, len). Simply dropping the # len part is safe: it is done that way in the normal # 'xx'.decode(encoding) code path. - # cfr python-source/Python/codecs.c:PyCodec_Decode return decoder(value, errors)[0] return process - # TODO: add scale argument - #def to_decimal_processor_factory(target_class): - # def process(value): - # if value is None: - # return None - # else: - # return target_class(str(value)) - # return process + def to_decimal_processor_factory(target_class, scale=10): + fstring = "%%.%df" % scale + + def process(value): + if value is None: + return None + else: + return target_class(fstring % value) + return process def to_float(value): if value is None: @@ -94,13 +99,3 @@ except ImportError: str_to_time = str_to_datetime_processor_factory(TIME_RE, datetime.time) str_to_date = str_to_datetime_processor_factory(DATE_RE, datetime.date) - -def to_decimal_processor_factory(target_class, scale=10): - fstring = "%%.%df" % scale - - def process(value): - if value is None: - return None - else: - return target_class(fstring % value) - return process |