[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.70,1.71 test_both.py,1.24,1.25
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
2002年12月03日 08:28:11 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv17593
Modified Files:
datetime.py test_both.py
Log Message:
The comments claimed that timedeltas can by multiplied by floats, but
neither the Python nor the C implementations supported that. It's
possible to allow it, but doesn't seem worth the bother now. So changed
the comments, and added tests to ensure that mixing datetime with a
float via * / // raises TypeError. Also refactored the timedelta
tests a little more (this started as one huge timedelta test; I've been
breaking it into smaller test pieces all along).
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.70
retrieving revision 1.71
diff -C2 -d -r1.70 -r1.71
*** datetime.py 2 Dec 2002 06:42:28 -0000 1.70
--- datetime.py 3 Dec 2002 16:27:51 -0000 1.71
***************
*** 256,260 ****
- unary plus, minus, abs
- compare to timedelta
! - multiply, divide by int/long/float
In addition, datetime supports subtraction of two datetime objects
--- 256,260 ----
- unary plus, minus, abs
- compare to timedelta
! - multiply, divide by int/long
In addition, datetime supports subtraction of two datetime objects
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** test_both.py 2 Dec 2002 23:47:20 -0000 1.24
--- test_both.py 3 Dec 2002 16:27:57 -0000 1.25
***************
*** 41,44 ****
--- 41,69 ----
class TestTimeDelta(unittest.TestCase):
+ def test_constructor(self):
+ eq = self.assertEqual
+ td = timedelta
+
+ # Check keyword args to constructor
+ eq(td(), td(weeks=0, days=0, hours=0, minutes=0, seconds=0,
+ milliseconds=0, microseconds=0))
+ eq(td(1), td(days=1))
+ eq(td(0, 1), td(seconds=1))
+ eq(td(0, 0, 1), td(microseconds=1))
+ eq(td(weeks=1), td(days=7))
+ eq(td(days=1), td(hours=24))
+ eq(td(hours=1), td(minutes=60))
+ eq(td(minutes=1), td(seconds=60))
+ eq(td(seconds=1), td(milliseconds=1000))
+ eq(td(milliseconds=1), td(microseconds=1000))
+
+ # Check float args to constructor
+ eq(td(weeks=1.0/7), td(days=1))
+ eq(td(days=1.0/24), td(hours=1))
+ eq(td(hours=1.0/60), td(minutes=1))
+ eq(td(minutes=1.0/60), td(seconds=1))
+ eq(td(seconds=0.001), td(milliseconds=1))
+ eq(td(milliseconds=0.001), td(microseconds=1))
+
def test_computations(self):
eq = self.assertEqual
***************
*** 80,83 ****
--- 105,111 ----
eq(a//3600000, td(0, 0, 7*24*1000))
+ def test_disallowed_computations(self):
+ a = timedelta(42)
+
# Add/sub ints, longs, floats should be illegal
for i in 1, 1L, 1.0:
***************
*** 87,108 ****
self.assertRaises(TypeError, lambda: i-a)
! # Check keyword args to constructor
! eq(td(1), td(days=1))
! eq(td(0, 1), td(seconds=1))
! eq(td(0, 0, 1), td(microseconds=1))
! eq(td(weeks=1), td(days=7))
! eq(td(days=1), td(hours=24))
! eq(td(hours=1), td(minutes=60))
! eq(td(minutes=1), td(seconds=60))
! eq(td(seconds=1), td(milliseconds=1000))
! eq(td(milliseconds=1), td(microseconds=1000))
!
! # Check float args to constructor
! eq(td(weeks=1.0/7), td(days=1))
! eq(td(days=1.0/24), td(hours=1))
! eq(td(hours=1.0/60), td(minutes=1))
! eq(td(minutes=1.0/60), td(seconds=1))
! eq(td(seconds=0.001), td(milliseconds=1))
! eq(td(milliseconds=0.001), td(microseconds=1))
def test_basic_attributes(self):
--- 115,126 ----
self.assertRaises(TypeError, lambda: i-a)
! # Mul/div by float isn't supported.
! x = 2.3
! self.assertRaises(TypeError, lambda: a*x)
! self.assertRaises(TypeError, lambda: x*a)
! self.assertRaises(TypeError, lambda: a/x)
! self.assertRaises(TypeError, lambda: x/a)
! self.assertRaises(TypeError, lambda: a // x)
! self.assertRaises(TypeError, lambda: x // a)
def test_basic_attributes(self):