tuple(int, int) --> FixedPoint; is there a better way?
Henrik Weber
Henrik.Weber at sys.aok.de
Mon Dec 2 06:12:18 EST 2002
Hello,
I have selected a value from a currency type column in MS Access using
ADO. The currency type in Access is a 64 bit integer with 4 decimal
places precision. Somewhere between the database and my program this
gets turned into a tuple of two 32 bit integers, the first one signed
and the second one unsigned.
Now I would like to turn that into a FixedPoint object, using Tim
Peters' fixedpoint library (http://fixedpoint.sourceforge.net).
For that I have to tell Python to interpret the second element of the
tuple as unsigned integer and combine both elements into a long
integer. Then I have to tell FixedPoint's initializer to regard the
last 4 decimal digits as the fraction. This is what I have come up
with so far:
import fixedpoint
class Money(fixedpoint.FixedPoint):
"""
Class adapting Access currency type to Python FixedPoint.
"""
def __init__(self, value=0):
"""
If value is a tuple with 2 elements, convert it to FixedPoint
value.
Otherwise pass it on to the initializer of the parent class.
"""
if (isinstance(value, types.TupleType) or isinstance(value,
types.ListType)) and len(value) == 2:
super(Money, self).__init__(0, 4)
self.n = value[0] * 2**32 + long("%u" % value[1])
else:
super(Money, self).__init__(value, 4)
Somehow I've got the feeling that there might be a more elegant/more
efficient way to do this.
Any ideas?
Thanks
Henrik
More information about the Python-list
mailing list