# KInterbasDB Python Package - Type Conv : Fixed/fixedpoint Module (3rd-party) # # Version 3.1 # # The following contributors hold Copyright (C) over their respective # portions of code (see license.txt for details): # # [Original Author (maintained through version 2.0-0.3.1):] # 1998-2001 [alex] Alexander Kuznetsov # [Maintainers (after version 2.0-0.3.1):] # 2001-2002 [maz] Marek Isalski # 2002-2004 [dsr] David Rushby # [Contributors:] # 2001 [eac] Evgeny A. Cherkashin # 2001-2002 [janez] Janez Jere __all__ = ( # kinterbasdb-native fixed point converters (old, precison_mode style): 'fixed_conv_in_imprecise', 'fixed_conv_in_precise', 'fixed_conv_out_imprecise', 'fixed_conv_out_precise', ) import sys, types from kinterbasdb.k_exceptions import * if sys.version_info < (2,2): from kinterbasdb.typeconv_util_isinstance import isinstance # With versions of Python that have at least rudimentary int/long unification # (2.2 and later), use ints where possible: if sys.version_info < (2,2): ten = 10L else: ten = 10 _tenTo = [ten**x for x in range(20)] del x, ten # With versions of Python that have extensive int/long unification (2.3 and # later), use int() rather than long() to construct the return value of the # input converter. # Python < 2.3's int() raises OverFlow error if the value exceeds the # capacity of a C long, whereas Python >= 2.3's int() handles the overflow # without complaint (by returning a Python long). if sys.version_info < (2,3): _int = long else: _int = int # The fixedpoint module resides at: http://fixedpoint.sourceforge.net/ from fixedpoint import FixedPoint ################################################################################ ## FIXED POINT ################################################################################ def fixed_conv_in_precise((val, scale)): # Allow implicit param conv: if val is None or isinstance(val, types.StringType): return val return _int(val * _tenTo[abs(scale)]) fixed_conv_in_imprecise = fixed_conv_in_precise def fixed_conv_out_precise(x): if x is None: return None absScale = abs(x[1]) return FixedPoint(x[0], absScale) / _tenTo[absScale] fixed_conv_out_imprecise = fixed_conv_out_precise