From: Billy G. A. <bal...@us...> - 2002-11-11 03:56:46
|
Update of /cvsroot/pypgsql/pypgsql/pyPgSQL In directory usw-pr-cvs1:/tmp/cvs-serv1419/pyPgSQL Modified Files: PgSQL.py Log Message: 10NOV2002 bga - Added the __hash__ function to the PgNumeric class. - Cleaned up the code in PgNumeric class and made some small improvments to it. Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** PgSQL.py 3 Nov 2002 19:48:48 -0000 1.21 --- PgSQL.py 11 Nov 2002 03:56:42 -0000 1.22 *************** *** 30,33 **** --- 30,36 ---- # Date Ini Description | # --------- --- ------------------------------------------------------- | + # 10NOV2002 bga - Added the __hash__ function to the PgNumeric class. | + # Cleaned up the code in PgNumeric class and made some | + # small improvments to it. | # 02NOV2002 bga - Added the PgArray class. This is a wrapper around a | # Python list and is used for all PostgreSQL arrays. | *************** *** 1270,1273 **** --- 1273,1281 ---- raise TypeError, "value can not be converted to a PgNumeric." + # The value (10L ** self.__s) is used a lot. Save it as a constant + # to save a (small) bit of time. + + self.__sf = 10L ** self.__s + def __fmtNumeric(self, value=None): # Check to see if the string representation of the python long has *************** *** 1330,1352 **** self, other = _c if self.__s < other.__s: ! _sc = other.__s ! _ds = other.__s - self.__s ! _s = self.__v * (10L ** _ds) _o = other.__v elif self.__s > other.__s: - _sc = self.__s - _ds = self.__s - other.__s _s = self.__v ! _o = other.__v * (10L ** _ds) else: - _sc = self.__s _s = self.__v _o = other.__v - if (self.__p - self.__s) >= (other.__p - other.__s): - _pr = self.__p - self.__s + _sc - else: - _pr = other.__p - other.__s + sc ! return PgNumeric((_s + _o), _pr, _sc) def __radd__(self, other): --- 1338,1359 ---- self, other = _c if self.__s < other.__s: ! _s = self.__v * (other.__sf / self.__sf) _o = other.__v elif self.__s > other.__s: _s = self.__v ! _o = other.__v * (self.__sf / other.__sf) else: _s = self.__v _o = other.__v ! mp = max(self.__p - self.__s, other.__p - other.__s) ! ms = max(self.__s, other.__s) ! v = _s + _o ! # Check to see if the addition caused an increase in the precision ! # due to a carry. If so, compensate for it. ! if (v / (10L ** (mp + ms))) > 0: ! mp = mp + 1 ! ! return PgNumeric((_s + _o), (mp + ms), ms) def __radd__(self, other): *************** *** 1357,1363 **** if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s return self --- 1364,1371 ---- if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s ! self.__sf = _r.__sf return self *************** *** 1368,1412 **** self, other = _c if self.__s < other.__s: ! _sc = other.__s ! _ds = other.__s - self.__s ! _s = self.__v * (10L ** _ds) _o = other.__v elif self.__s > other.__s: - _sc = self.__s - _ds = self.__s - other.__s _s = self.__v ! _o = other.__v * (10L ** _ds) else: - _sc = self.__s _s = self.__v _o = other.__v - if (self.__p - self.__s) >= (other.__p - other.__s): - _pr = self.__p - self.__s + _sc - else: - _pr = other.__p - other.__s + sc ! return PgNumeric((_s - _o), _pr, _sc) def __rsub__(self, other): ! if self.__s < other.__s: ! _sc = other.__s ! _ds = other.__s - self.__s ! _s = self.__v * (10L ** _ds) ! _o = other.__v ! elif self.__s > other.__s: ! _sc = self.__s ! _ds = self.__s - other.__s ! _s = self.__v ! _o = other.__v * (10L ** _ds) ! else: ! _sc = self.__s ! _s = self.__v ! _o = other.__v ! if (self.__p - self.__s) >= (other.__p - other.__s): ! _pr = self.__p - self.__s + _sc ! else: ! _pr = other.__p - other.__s + sc ! ! return PgNumeric((_o - _s), _pr, _sc) def __isub__(self, other): --- 1376,1394 ---- self, other = _c if self.__s < other.__s: ! _s = self.__v * (other.__sf / self.__sf) _o = other.__v elif self.__s > other.__s: _s = self.__v ! _o = other.__v * (self.__sf / other.__sf) else: _s = self.__v _o = other.__v ! mp = max(self.__p - self.__s, other.__p - other.__s) ! ms = max(self.__s, other.__s) ! return PgNumeric((_s - _o), (mp + ms), ms) def __rsub__(self, other): ! return self.__sub__(other, self) def __isub__(self, other): *************** *** 1414,1420 **** if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s return self --- 1396,1403 ---- if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s ! self.__sf = _r.__sf return self *************** *** 1434,1440 **** if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s return self --- 1417,1424 ---- if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s ! self.__sf = _r.__sf return self *************** *** 1444,1461 **** return None self, other = _c ! _n = (self.__v * (10L ** (other.__s + 1))) _d = other.__v ! _q = (_n / _d) ! return PgNumeric(self._round(_q, 1), self.__p, self.__s) def __rdiv__(self, other): ! _c = self.__coerce__(other) ! if _c is None: ! return None ! self, other = _c ! _n = (other.__v * (10L ** (self.__s + 1))) ! _d = self.__v ! _q = (_n / _d) ! return PgNumeric(self._round(_q, 1), other.__p, other.__s) def __idiv__(self, other): --- 1428,1438 ---- return None self, other = _c ! _n = self.__v * other.__sf * self.__sf _d = other.__v ! _q = self._round((_n / _d), self.__s) ! return PgNumeric(_q, self.__p, self.__s) def __rdiv__(self, other): ! return self.__div__(other, self) def __idiv__(self, other): *************** *** 1463,1469 **** if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s return self --- 1440,1447 ---- if _r is None: return None ! self.__v = _r.__v ! self.__p = _r.__p ! self.__s = _r.__s ! self.__sf = _r.__sf return self *************** *** 1476,1486 **** self, other = _c if self.__s < other.__s: ! _d = other.__s - self.__s ! _s = (self.__v * (10L ** _d)) _o = other.__v elif self.__s > other.__s: - _d = self.__s - other.__s _s = self.__v ! _o = (other.__v * (10L ** _d)) else: _s = self.__v --- 1454,1462 ---- self, other = _c if self.__s < other.__s: ! _s = self.__v * (other.__sf / self.__sf) _o = other.__v elif self.__s > other.__s: _s = self.__v ! _o = other.__v * (self.__sf / other.__sf) else: _s = self.__v *************** *** 1495,1505 **** def __pos__(self): ! return self def __abs__(self): if self.__v >= 0: ! return self else: ! return -self def __quote__(self, forArray=0): --- 1471,1481 ---- def __pos__(self): ! return PgNumeric(self.__v, self.__p, self.__s) def __abs__(self): if self.__v >= 0: ! return PgNumeric(self.__v, self.__p, self.__s) else: ! return PgNumeric(-self.__v, self.__p, self.__s) def __quote__(self, forArray=0): *************** *** 1512,1527 **** def __int__(self): ! return int(self.__v / (10L ** self.__s)) def __long__(self): ! return self.__v / (10L ** self.__s) def __float__(self): v = self.__v ! s = 10L ** self.__s return (float(v / s) + (float(v % s) / float(s))) def __complex__(self): return complex(self.__float__()) #-----------------------------------------------------------------------+ --- 1488,1511 ---- def __int__(self): ! return int(self.__v / self.__sf) def __long__(self): ! return self.__v / self.__sf def __float__(self): v = self.__v ! s = self.__sf return (float(v / s) + (float(v % s) / float(s))) def __complex__(self): return complex(self.__float__()) + + def __hash__(self): + if self.__s == 0: + return hash(self.__v) + v = self.__v / self.__sf + if (v * self.__sf) == self.__v: + return hash(v) + return hash(float(self)) #-----------------------------------------------------------------------+ |