From 90942c9d3e422128c77471f0aa3c074d8e529977 Mon Sep 17 00:00:00 2001 From: Tomas Kazmar Date: Fri, 22 May 2015 14:07:13 +0200 Subject: [PATCH 1/3] Fix csv2rec for passing in both names and comments. --- lib/matplotlib/mlab.py | 11 ++++++++--- lib/matplotlib/tests/test_mlab.py | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 083e13e36c7d..c830db8e775c 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2867,12 +2867,17 @@ def get_func(name, item, func): 'print': 'print_', } - def get_converters(reader): + def get_converters(reader, comments): converters = None - for i, row in enumerate(reader): + i = 0 + for row in reader: + if (len(row) and comments is not None and + row[0].startswith(comments)): + continue if i == 0: converters = [mybool]*len(row) + i += 1 if checkrows and i > checkrows: break for j, (name, item) in enumerate(zip(names, row)): @@ -2925,7 +2930,7 @@ def get_converters(reader): names = [n.strip() for n in names.split(',')] # get the converter functions by inspecting checkrows - converters = get_converters(reader) + converters = get_converters(reader, comments) if converters is None: raise ValueError('Could not find any valid data in CSV file') diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 51652a49ef52..f703dce47566 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -339,6 +339,14 @@ def test_rec2csv_bad_shape_ValueError(self): # the bad recarray should trigger a ValueError for having ndim > 1. assert_raises(ValueError, mlab.rec2csv, bad, self.fd) + def test_csv2rec_names_with_comments(self): + self.fd.seek(0) + self.fd.write('# comment\n1,2,3\n4,5,6\n') + self.fd.seek(0) + array = mlab.csv2rec(self.fd, names='a,b,c') + assert len(array) == 2 + assert len(array.dtype) == 3 + class window_testcase(CleanupTestCase): def setUp(self): From 1c7c59840dfbce1793ee887e5fbf6652db4c09fe Mon Sep 17 00:00:00 2001 From: Tomas Kazmar Date: Fri, 22 May 2015 14:21:44 +0200 Subject: [PATCH 2/3] Patch wrong checkrows. --- lib/matplotlib/mlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index c830db8e775c..0a270b1b481f 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2877,9 +2877,9 @@ def get_converters(reader, comments): continue if i == 0: converters = [mybool]*len(row) - i += 1 if checkrows and i > checkrows: break + i += 1 for j, (name, item) in enumerate(zip(names, row)): func = converterd.get(j) if func is None: From 634de83b5841d9b6237f4888fc4a9edd42c50352 Mon Sep 17 00:00:00 2001 From: Tomas Kazmar Date: Fri, 22 May 2015 16:30:49 +0200 Subject: [PATCH 3/3] fix --- lib/matplotlib/mlab.py | 1 + lib/matplotlib/tests/test_mlab.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 0a270b1b481f..cc2811438798 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2880,6 +2880,7 @@ def get_converters(reader, comments): if checkrows and i > checkrows: break i += 1 + for j, (name, item) in enumerate(zip(names, row)): func = converterd.get(j) if func is None: diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index f703dce47566..71d3a92aec37 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -340,7 +340,6 @@ def test_rec2csv_bad_shape_ValueError(self): assert_raises(ValueError, mlab.rec2csv, bad, self.fd) def test_csv2rec_names_with_comments(self): - self.fd.seek(0) self.fd.write('# comment\n1,2,3\n4,5,6\n') self.fd.seek(0) array = mlab.csv2rec(self.fd, names='a,b,c')