[Scipy-svn] r2664 - in trunk/Lib/sandbox/timeseries: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Jan 31 22:48:44 EST 2007


Author: pierregm
Date: 2007-01-31 21:48:41 -0600 (Wed, 31 Jan 2007)
New Revision: 2664

Modified:
   trunk/Lib/sandbox/timeseries/tdates.py
   trunk/Lib/sandbox/timeseries/tests/test_dates.py
Log:
tdates:added some shortcuts for the creation of Date and DateArray objects
tdates:solved an issue in Date when parsing a string as value

Modified: trunk/Lib/sandbox/timeseries/tdates.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tdates.py	2007-01-31 19:01:50 UTC (rev 2663)
+++ trunk/Lib/sandbox/timeseries/tdates.py	2007-02-01 03:48:41 UTC (rev 2664)
@@ -33,7 +33,6 @@
 
 
 
-
 __all__ = [
 'Date', 'DateArray','isDate','isDateArray',
 'DateError', 'ArithmeticDateError', 'FrequencyDateError','InsufficientDateError',
@@ -135,9 +134,10 @@
             self.freq = corelib.check_freq(freq)
         self.freqstr = corelib.freq_tostr(self.freq)
         
+        
         if value is not None:
             if isinstance(value, str):
-                self.mxDate = mxDFromString(string)
+                self.mxDate = mxDFromString(value)
             elif self.freqstr == 'A':
                 self.mxDate = mxD.Date(value, -1, -1)
             elif self.freqstr == 'B':
@@ -474,7 +474,8 @@
             
 def isDate(data):
     "Returns whether `data` is an instance of Date."
-    return isinstance(data, Date)
+    return isinstance(data, Date) or \
+           (hasattr(data,'freq') and hasattr(data,'value'))
 
             
 #####---------------------------------------------------------------------------
@@ -882,22 +883,34 @@
     if dlist is not None:
         # Already a DateArray....................
         if isinstance(dlist, DateArray):
-            if freq != dlist.freq:
+            if (freq is not None) and (dlist.freq != corelib.check_freq(freq)):
                 return dlist.asfreq(freq)
             else:
                 return dlist
-        return _listparser(dlist, freq)
+        # Make sure it's a sequence, else that's a start_date
+        if hasattr(dlist,'__len__'):
+            return _listparser(dlist, freq)
+        elif start_date is not None:
+            if end_date is not None:
+                dmsg = "What starting date should be used ? '%s' or '%s' ?"
+                raise DateError, dmsg % (dlist, start_date)
+            else:
+                (start_date, end_date) = (dlist, start_date)
+        else:
+            start_date = dlist
     # Case #2: we have a starting date ..........
     if start_date is None:
         raise InsufficientDateError
-    if not isinstance(start_date, Date):
-        raise DateError, "Starting date should be a valid Date instance!"
+    if not isDate(start_date):
+        dmsg = "Starting date should be a valid Date instance! "
+        dmsg += "(got '%s' instead)" % type(start_date)
+        raise DateError, dmsg
     # Check if we have an end_date
     if end_date is None:
         if length is None:
             raise ValueError,"No length precised!"
     else:
-        if not isinstance(end_date, Date):
+        if not isDate(end_date):
             raise DateError, "Ending date should be a valid Date instance!"
         length = end_date - start_date
         if include_last:
@@ -963,3 +976,8 @@
 
 
 ################################################################################
+
+if __name__ == '__main__':
+    assert (Date('D','2007-01')==Date('D',string='2007-01'))
+    assert (Date('D','2007-01')==Date('D', value=732677))
+    assert (Date('D',732677)==Date('D', value=732677))
\ No newline at end of file

Modified: trunk/Lib/sandbox/timeseries/tests/test_dates.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tests/test_dates.py	2007-01-31 19:01:50 UTC (rev 2663)
+++ trunk/Lib/sandbox/timeseries/tests/test_dates.py	2007-02-01 03:48:41 UTC (rev 2664)
@@ -29,7 +29,8 @@
 #reload(tdates)
 from timeseries import tcore
 #reload(tcore)
-from timeseries.tdates import date_array_fromlist, Date, DateArray, date_array, mxDFromString
+from timeseries.tdates import date_array_fromlist, Date, DateArray, date_array,\
+    mxDFromString, today
 
 class test_creation(NumpyTestCase):
     "Base test class for MaskedArrays."
@@ -111,8 +112,19 @@
         for f in freqs:
             today = tdates.thisday(f)
             assert(tdates.Date(freq=f, value=today.value) == today)
+            
+    def test_shortcuts(self):
+        "Tests some creation shortcuts. Because I'm lazy like that."
+        # Dates shortcuts
+        assert_equal(Date('D','2007-01'), Date('D',string='2007-01'))
+        assert_equal(Date('D','2007-01'), Date('D', value=732677))
+        assert_equal(Date('D',732677), Date('D', value=732677))
+        # DateArray shortcuts
+        n = today('M')
+        d = date_array(start_date=n, length=3)
+        assert_equal(date_array(n,length=3), d)
+        assert_equal(date_array(n, n+2), d)
 
-
 class test_date_properties(NumpyTestCase):
     "Test properties such as year, month, day_of_week, etc...."
     




More information about the Scipy-svn mailing list