From: <as...@us...> - 2007-12-16 23:20:01
|
Revision: 4749 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4749&view=rev Author: astraw Date: 2007-12-16 15:19:59 -0800 (Sun, 16 Dec 2007) Log Message: ----------- fix csv2rec roundtrip for funky strings, too 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-16 20:53:35 UTC (rev 4748) +++ trunk/matplotlib/lib/matplotlib/mlab.py 2007-12-16 23:19:59 UTC (rev 4749) @@ -2236,11 +2236,15 @@ return repr(x) +class FormatString2(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 FormatFormatStr(FormatObj): def __init__(self, fmt): self.fmt = fmt @@ -2297,7 +2301,7 @@ npy.float32 : FormatFloat(), npy.float64 : FormatFloat(), npy.object_ : FormatObj(), - npy.string_ : FormatObj(), + npy.string_ : FormatString2(), } def get_formatd(r, formatd=None): Modified: trunk/matplotlib/unit/mlab_unit.py =================================================================== --- trunk/matplotlib/unit/mlab_unit.py 2007-12-16 20:53:35 UTC (rev 4748) +++ trunk/matplotlib/unit/mlab_unit.py 2007-12-16 23:19:59 UTC (rev 4749) @@ -13,25 +13,34 @@ self.failIf( fh.closed ) def test_csv2rec_roundtrip(self): - # Make sure double-precision floats pass through. + # Make sure double-precision floats and strings pass through a + # roundtrip unaltered. + # A bug in numpy (fixed in r4602) meant that numpy scalars # 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), (456, 123.456)], - dtype=[('a', '<i8'), ('b', '<f8')]) - rec2csv_closes_files = True - if rec2csv_closes_files: - fh = 'mlab_unit_tmp.csv' - else: - fh = StringIO.StringIO() + ra=numpy.rec.array([(123, 1197346475.0137341, 'a,bc'), + (456, 123.456, 'd\'ef'), + (789, 0.000000001, 'ghi'), + ], + dtype=[('a', '<i8'), ('b', '<f8'), ('c', '|S3')]) + fh = StringIO.StringIO() mlab.rec2csv( ra, fh ) - if not rec2csv_closes_files: + fh.seek(0) + if 0: + print 'CSV contents:','-'*40 + print fh.read() + print '-'*40 fh.seek(0) ra2 = mlab.csv2rec(fh) + fh.close() for name in ra.dtype.names: - #print name, repr(ra[name]), repr(ra2[name]) + if 0: + print name, repr(ra[name]), repr(ra2[name]) + dt = ra.dtype[name] + print 'repr(dt.type)',repr(dt.type) self.failUnless( numpy.all(ra[name] == ra2[name]) ) # should not fail with numpy 1.0.5 if __name__=='__main__': This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |