From: <jd...@us...> - 2007-12-17 04:37:43
|
Revision: 4755 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4755&view=rev Author: jdh2358 Date: 2007-12-16 20:37:38 -0800 (Sun, 16 Dec 2007) Log Message: ----------- mods to support dates in csv2rec and friends Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/mlab.py trunk/matplotlib/unit/mlab_unit.py Modified: trunk/matplotlib/lib/matplotlib/mlab.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mlab.py 2007-12-17 02:57:17 UTC (rev 4754) +++ trunk/matplotlib/lib/matplotlib/mlab.py 2007-12-17 04:37:38 UTC (rev 4755) @@ -2129,6 +2129,7 @@ process_skiprows(reader) + dateparser = dateutil.parser.parse def myfloat(x): if x==missing: @@ -2136,9 +2137,18 @@ else: return float(x) + def mydate(x): + # try and return a date object + d = dateparser(x) + + if d.hour>0 or d.minute>0 or d.second>0: + raise ValueError('not a date') + return d.date() + + def get_func(item, func): # promote functions in this order - funcmap = {int:myfloat, myfloat:dateutil.parser.parse, dateutil.parser.parse:str} + funcmap = {int:myfloat, myfloat:mydate, mydate:dateparser, dateparser:str} try: func(item) except: if func==str: @@ -2233,17 +2243,17 @@ return self.toval(x) def toval(self, x): - return repr(x) + return str(x) -class FormatString2(FormatObj): +class FormatString(FormatObj): def tostr(self, x): val = repr(x) return val[1:-1] -class FormatString(FormatObj): - def tostr(self, x): - return '"%r"'%self.toval(x) +#class FormatString(FormatObj): +# def tostr(self, x): +# return '"%r"'%self.toval(x) class FormatFormatStr(FormatObj): def __init__(self, fmt): @@ -2301,7 +2311,7 @@ npy.float32 : FormatFloat(), npy.float64 : FormatFloat(), npy.object_ : FormatObj(), - npy.string_ : FormatString2(), + npy.string_ : FormatString(), } def get_formatd(r, formatd=None): Modified: trunk/matplotlib/unit/mlab_unit.py =================================================================== --- trunk/matplotlib/unit/mlab_unit.py 2007-12-17 02:57:17 UTC (rev 4754) +++ trunk/matplotlib/unit/mlab_unit.py 2007-12-17 04:37:38 UTC (rev 4755) @@ -1,7 +1,6 @@ -import unittest +import datetime, StringIO, unittest import matplotlib.mlab as mlab import numpy -import StringIO class TestMlab(unittest.TestCase): def test_csv2rec_closefile(self): @@ -21,11 +20,22 @@ # lost precision when passing through repr(). csv2rec was # affected by this. This test will only pass on numpy >= # 1.0.5. - ra=numpy.rec.array([(123, 1197346475.0137341, 'a,bc'), - (456, 123.456, 'd\'ef'), - (789, 0.000000001, 'ghi'), + delta = datetime.timedelta(days=1) + date0 = datetime.date(2007,12,16) + date1 = date0 + delta + date2 = date1 + delta + + delta = datetime.timedelta(days=1) + datetime0 = datetime.datetime(2007,12,16,22,29,34,924122) + datetime1 = datetime0 + delta + datetime2 = datetime1 + delta + ra=numpy.rec.fromrecords([ + (123, date0, datetime0, 1197346475.0137341, 'a,bc'), + (456, date1, datetime1, 123.456, 'd\'ef'), + (789, date2, datetime2, 0.000000001, 'ghi'), ], - dtype=[('a', '<i8'), ('b', '<f8'), ('c', '|S3')]) + names='intdata,datedata,datetimedata,floatdata,stringdata') + fh = StringIO.StringIO() mlab.rec2csv( ra, fh ) fh.seek(0) @@ -36,6 +46,8 @@ fh.seek(0) ra2 = mlab.csv2rec(fh) fh.close() + #print 'ra', ra + #print 'ra2', ra2 for name in ra.dtype.names: if 0: print name, repr(ra[name]), repr(ra2[name]) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |