[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.25,
1.26 test_Decimal.py, 1.20, 1.21
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Tue Jun 29 16:49:53 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14187
Modified Files:
Decimal.py test_Decimal.py
Log Message:
* Remove from_float() from the API
* copy and deepcopy produce new instances for subclasses
* use assertRaises instead of try/except in tests
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** Decimal.py 29 Jun 2004 10:08:35 -0000 1.25
--- Decimal.py 29 Jun 2004 20:49:50 -0000 1.26
***************
*** 394,399 ****
Decimal instance
long, int
-
- To construct from float, use Decimal.from_float(float_value)
"""
if context is None:
--- 394,397 ----
***************
*** 472,509 ****
return
- if isinstance(value, float):
- raise TypeError("Can't convert " + repr(value) +
- ". Try Decimal.from_float() instead.")
-
raise TypeError("Can't convert %r" % value)
- def from_float(cls, value, positions=None):
- """Class method that creates Decimal from float
-
- value must be float
- if positions is present, rounds to that quantity of decimal places
- """
- if not isinstance(value, float):
- raise TypeError, "value must be float in Decimal.from_float(value, [positions])"
-
- # get the *very* exact string representation of the float
- string_number = _floatToString(value)
- d = cls(string_number)
-
- # round up to positions
- if positions is not None:
- if not isinstance(positions, (int,long)):
- raise TypeError, "positions must be int or long in Decimal.from_float(value, [positions])"
- if positions < 0:
- raise TypeError, "positions must be not negative in Decimal.from_float(value, [positions])"
-
- # we must know what precision to pass to round
- int_positions = len(d._int) + d._exp
- real_decimal_positions = positions + int_positions
- d = d._round(real_decimal_positions, ROUND_HALF_UP)
-
- return d
- from_float = classmethod(from_float)
-
def _convert_other(self, other):
"""Convert other to Decimal.
--- 470,475 ----
***************
*** 2096,2103 ****
def __copy__(self):
! return self # I'm immutable; therefore I am my own clone
def __deepcopy__(self, memo):
! return self # My components are also immutable
--- 2062,2073 ----
def __copy__(self):
! if type(self) == Decimal:
! return self # I'm immutable; therefore I am my own clone
! return self.__class__(str(self))
def __deepcopy__(self, memo):
! if type(self) == Decimal:
! return self # My components are also immutable
! return self.__class__(str(self))
***************
*** 2240,2247 ****
def create_decimal(self, num):
"""Creates a new Decimal instance but using self as context."""
! if isinstance(num, float):
! d = Decimal.from_float(num)
! else:
! d = Decimal(num, context=self)
return d._fix(context=self)
--- 2210,2214 ----
def create_decimal(self, num):
"""Creates a new Decimal instance but using self as context."""
! d = Decimal(num, context=self)
return d._fix(context=self)
Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** test_Decimal.py 29 Jun 2004 10:08:35 -0000 1.20
--- test_Decimal.py 29 Jun 2004 20:49:51 -0000 1.21
***************
*** 516,559 ****
self.assertEqual(str(d), '4.5E+3')
#just not a number
d = Decimal('ugly')
self.assertEqual(str(d), 'NaN')
- def test_from_float(self):
- '''Explicit construction with float.'''
-
- #positive integer
- d = Decimal.from_float(45.0)
- self.assertEqual(str(d), '45')
-
- #negative integer
- d = Decimal.from_float(-32.0)
- self.assertEqual(str(d), '-32')
-
- #zero
- d = Decimal.from_float(0.0)
- self.assertEqual(str(d), '0')
-
- #empty
- self.assertRaises(TypeError, Decimal.from_float)
-
- #with a string
- self.assertRaises(TypeError, Decimal.from_float, '')
-
- #with an int
- self.assertRaises(TypeError, Decimal.from_float, 5)
-
- #inexact float, without positions
- d = Decimal.from_float(2.2)
- self.assertEqual(str(d), '2.20000000000000017763568394002504646778106689453125')
-
- #inexact float, rounded to some positions
- d = Decimal.from_float(2.2, 16)
- self.assertEqual(str(d), '2.2000000000000002')
-
- #inexact float, rounded to less positions
- d = Decimal.from_float(2.2, 5)
- self.assertEqual(str(d), '2.20000')
-
def test_from_tuples(self):
'''Explicit construction with tuples.'''
--- 516,524 ----
self.assertEqual(str(d), '4.5E+3')
+ # XXX this should raise a ValueError for an invalid literal
#just not a number
d = Decimal('ugly')
self.assertEqual(str(d), 'NaN')
def test_from_tuples(self):
'''Explicit construction with tuples.'''
***************
*** 637,646 ****
self.assertEqual(str(d), '4.57E+5')
- # from float
- d = Decimal.from_float(1.1)
- self.assertEqual(str(d), '1.100000000000000088817841970012523233890533447265625')
- d = nc.create_decimal(1.1)
- self.assertEqual(str(d), '1.10')
-
# from tuples
d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) )
--- 602,605 ----
***************
*** 663,722 ****
'''Implicit construction with None.'''
! d = Decimal(5)
! try:
! d + None
! except TypeError:
! pass
! else:
! self.fail('Did not raised an error!')
def test_from_int(self):
'''Implicit construction with int or long.'''
- d = Decimal(5)
#normal
! e = d + 45
! self.assertEqual(str(e), '50')
#exceeding precision
! try:
! d + 123456789000
! except ValueError:
! pass
! else:
! self.fail('Did not raised an error!')
def test_from_string(self):
'''Implicit construction with string.'''
- d = Decimal(5)
#just any string
! try:
! d + '3'
! except TypeError:
! pass
! else:
! self.fail('Did not raised an error!')
def test_from_float(self):
'''Implicit construction with float.'''
- d = Decimal(5)
#just any float
! try:
! d + 2.2
! except TypeError:
! pass
! else:
! self.fail('Did not raised an error!')
def test_from_Decimal(self):
'''Implicit construction with Decimal.'''
! d = Decimal(5)
!
! #any Decimal
! e = d + Decimal(45)
! self.assertEqual(str(e), '50')
--- 622,652 ----
'''Implicit construction with None.'''
! self.assertRaises(TypeError, eval, 'Decimal(5) + None', globals())
def test_from_int(self):
'''Implicit construction with int or long.'''
#normal
! self.assertEqual(str(Decimal(5) + 45), '50')
#exceeding precision
! self.assertRaises(ValueError, eval, 'Decimal(5) + 123456789000', globals())
def test_from_string(self):
'''Implicit construction with string.'''
#just any string
! self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', globals())
def test_from_float(self):
'''Implicit construction with float.'''
#just any float
! self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', globals())
def test_from_Decimal(self):
'''Implicit construction with Decimal.'''
! self.assertEqual(Decimal(5) + Decimal(45), Decimal(50))
***************
*** 1281,1288 ****
test_classes = []
! if which == "Arithmetic" or which is None:
test_classes.extend([DecimalTest])
! if which == "Behaviour" or which is None:
test_classes.extend([
DecimalExplicitConstructionTest,
--- 1211,1218 ----
test_classes = []
! if which is None or which.lower().startswith("arith"):
test_classes.extend([DecimalTest])
! if which is None or which.lower().startswith("behav"):
test_classes.extend([
DecimalExplicitConstructionTest,
More information about the Python-checkins
mailing list