[Numpy-svn] r5860 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Sep 22 15:28:25 CDT 2008


Author: stefan
Date: 2008-09-22 15:28:00 -0500 (Mon, 22 Sep 2008)
New Revision: 5860

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
Ignore unused converters in `loadtxt`.


Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-09-22 03:21:01 UTC (rev 5859)
+++ trunk/numpy/lib/io.py	2008-09-22 20:28:00 UTC (rev 5860)
@@ -378,7 +378,11 @@
     # By preference, use the converters specified by the user
     for i, conv in (user_converters or {}).iteritems():
         if usecols:
-            i = usecols.index(i)
+            try:
+                i = usecols.index(i)
+            except ValueError:
+                # Unused converter specified
+                continue
         converters[i] = conv
 
     # Parse each line, including the first

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-09-22 03:21:01 UTC (rev 5859)
+++ trunk/numpy/lib/tests/test_io.py	2008-09-22 20:28:00 UTC (rev 5860)
@@ -258,6 +258,17 @@
         c = StringIO.StringIO()
         assert_raises(IOError, np.loadtxt, c)
 
+    def test_unused_converter(self):
+        c = StringIO.StringIO()
+        c.writelines(['1 21\n', '3 42\n'])
+        c.seek(0)
+        data = np.loadtxt(c, usecols=(1,), converters={0: lambda s: int(s, 16)})
+        assert_array_equal(data, [21, 42])
+
+        c.seek(0)
+        data = np.loadtxt(c, usecols=(1,), converters={1: lambda s: int(s, 16)})
+        assert_array_equal(data, [33, 66])
+
 class Testfromregex(TestCase):
     def test_record(self):
         c = StringIO.StringIO()



More information about the Numpy-svn mailing list