[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.17, 1.18

facundobatista at users.sourceforge.net facundobatista at users.sourceforge.net
Sun Apr 4 12:14:27 EDT 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27407

Modified Files:
	Decimal.py 
Log Message:
Added _convert_other method, for implicit constructions. Added more checks when constructing from tuples. Fixed __sub__ to modify a copy of the instance.

Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** Decimal.py	4 Apr 2004 05:00:13 -0000	1.17
--- Decimal.py	4 Apr 2004 16:14:25 -0000	1.18
***************
*** 96,99 ****
--- 96,102 ----
  """
  
+ # 0.7.3     2003.4.04  facundobatista: Added _convert_other method, for implicit
+ #                      constructions, and more checks when constructing from
+ #                      tuples. Fixed __sub__ to modify a copy of the instance.
  # 0.7.2     2003.4.02  facundobatista: Fixed __pow__ to run the example ok. Added
  #                      from_float method. Fixed isnan to accept empty strings.
***************
*** 407,411 ****
          list/tuple in form (sign, (digit_list), exponent)
          Decimal instance
!         float, long, int
          """
          if context is None:
--- 410,416 ----
          list/tuple in form (sign, (digit_list), exponent)
          Decimal instance
!         long, int
! 
!         To construct from float, use Decimal.from_float(float_value)
          """
          if context is None:
***************
*** 454,459 ****
              if value[0] not in [0,1]:
                  raise ValueError, 'Invalid sign'
              
-             # FIXME: Add more input validation checks here
              self._sign = value[0]
              self._int  = tuple(value[1])
--- 459,466 ----
              if value[0] not in [0,1]:
                  raise ValueError, 'Invalid sign'
+             for digit in value[1]:
+                 if not isinstance(digit, (int,long)) or digit < 0:
+                     raise ValueError, "The second value in the tuple must be composed of non negative integer elements."
              
              self._sign = value[0]
              self._int  = tuple(value[1])
***************
*** 513,516 ****
--- 520,536 ----
      from_float = classmethod(from_float)
  
+     def _convert_other(self, other):
+         """Convert other to Decimal.
+ 
+         Verifies that it's ok to use in an implicit construction.
+         """
+         if isinstance(other, Decimal):
+             return other
+         if isinstance(other, (int, long)):
+             other = Decimal(other)
+             return other
+ 
+         raise TypeError, "You can interact Decimal only with int, long or Decimal data types."
+ 
      def _isnan(self):
          """Returns whether the number is not actually one.
***************
*** 593,601 ****
          if context is None:
              context = getcontext()
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
- 
          if ans:
              return 1
--- 613,619 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
          if ans:
              return 1
***************
*** 654,661 ****
          if context is None:
              context = getcontext()
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
!         ans = self._check_nans(other, context)
          #compare(NaN, NaN) = NaN
          if ans:
              return ans
--- 672,679 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
! 
          #compare(NaN, NaN) = NaN
+         ans = self._check_nans(other, context)
          if ans:
              return ans
***************
*** 850,856 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 868,872 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 957,967 ****
          if context is None:
              context = getcontext()
          ans = self._check_nans(other, context=context)
          if ans:
              return ans
          # -Decimal(0) = Decimal(0), which we don't want since
          # (-0 - 0 = -0 + (-0) = -0, but -0 + 0 = 0.)
!         # so we change the sign directly.
!         tmp = Decimal(other)
          tmp._sign = 1-tmp._sign
  
--- 973,986 ----
          if context is None:
              context = getcontext()
+         tmp = self._convert_other(other)
+ 
          ans = self._check_nans(other, context=context)
          if ans:
              return ans
+ 
          # -Decimal(0) = Decimal(0), which we don't want since
          # (-0 - 0 = -0 + (-0) = -0, but -0 + 0 = 0.)
!         # so we change the sign directly to a copy
!         tmp = Decimal(tmp)
          tmp._sign = 1-tmp._sign
  
***************
*** 969,976 ****
  
      def __rsub__(self, other, context=None):
          if context is None:
              context = getcontext()
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
          tmp = Decimal(self)
          tmp._sign = 1 - tmp._sign
--- 988,996 ----
  
      def __rsub__(self, other, context=None):
+         """Return other + (-self)"""
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
! 
          tmp = Decimal(self)
          tmp._sign = 1 - tmp._sign
***************
*** 1015,1021 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 1035,1039 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 1107,1113 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 1125,1129 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 1284,1290 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 1300,1304 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 1306,1312 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 1320,1324 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 1699,1705 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(n, Decimal):
!             n = Decimal(n)
  
          #Because the spot << doesn't work with really big exponents
--- 1711,1715 ----
          if context is None:
              context = getcontext()
!         n = self._convert_other(n)
  
          #Because the spot << doesn't work with really big exponents
***************
*** 2046,2052 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 2056,2060 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)
***************
*** 2070,2076 ****
          if context is None:
              context = getcontext()
! 
!         if not isinstance(other, Decimal):
!             other = Decimal(other)
  
          ans = self._check_nans(other, context)
--- 2078,2082 ----
          if context is None:
              context = getcontext()
!         other = self._convert_other(other)
  
          ans = self._check_nans(other, context)




More information about the Python-checkins mailing list