tuple(int, int) --> FixedPoint; is there a better way?

Piet van Oostrum piet at cs.uu.nl
Mon Dec 2 06:53:32 EST 2002


>>>>> Henrik.Weber at sys.aok.de (Henrik Weber) (HW) writes:

HW> Hello,
HW> I have selected a value from a currency type column in MS Access using
HW> ADO. The currency type in Access is a 64 bit integer with 4 decimal
HW> places precision. Somewhere between the database and my program this
HW> gets turned into a tuple of two 32 bit integers, the first one signed
HW> and the second one unsigned.

HW> Now I would like to turn that into a FixedPoint object, using Tim
HW> Peters' fixedpoint library (http://fixedpoint.sourceforge.net).

HW> For that I have to tell Python to interpret the second element of the
HW> tuple as unsigned integer and combine both elements into a long
HW> integer. Then I have to tell FixedPoint's initializer to regard the
HW> last 4 decimal digits as the fraction. This is what I have come up
HW> with so far:

HW> import fixedpoint

HW> class Money(fixedpoint.FixedPoint):
HW>     """
HW>     Class adapting Access currency type to Python FixedPoint.
HW>     """
HW>     def __init__(self, value=0):
HW>         """
HW>         If value is a tuple with 2 elements, convert it to FixedPoint
HW> value.
HW>         Otherwise pass it on to the initializer of the parent class.
HW>         """
HW>         if (isinstance(value, types.TupleType) or isinstance(value,
HW> types.ListType)) and len(value) == 2:
HW>             super(Money, self).__init__(0, 4)
HW>             self.n = value[0] * 2**32 + long("%u" % value[1])
HW>         else:
HW>             super(Money, self).__init__(value, 4)



HW> Somehow I've got the feeling that there might be a more elegant/more
HW> efficient way to do this.

HW> Any ideas?

if value[1] < 0 then add 2**32 to it.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list