summaryrefslogtreecommitdiff
path: root/numpy/lib/_iotools.py
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2009-10-06 03:47:07 +0000
committerpierregm <pierregm@localhost>2009-10-06 03:47:07 +0000
commit6386708bcd6deaf3f6e5f145fb59c0d4b9af86d9 (patch)
treec5ef08d00870e076b37298cf37c9bb4ca868d5fb /numpy/lib/_iotools.py
parent9a41e774079340e1a8887d9f2310c2458580ec6c (diff)
downloadnumpy-6386708bcd6deaf3f6e5f145fb59c0d4b9af86d9.tar.gz
* _iotools.StringConverter
- use '1' instead of '0' to test the update - add `iterupgrade` to upgrade from an iterator * io.genfromtxt (bug #1212) - use `iterupgrade` to upgrade the converters, and reprocess if there's a problem to catch the offending line
Diffstat (limited to 'numpy/lib/_iotools.py')
-rw-r--r--numpy/lib/_iotools.py37
1 files changed, 33 insertions, 4 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py
index c06275d1b..02385305b 100644
--- a/numpy/lib/_iotools.py
+++ b/numpy/lib/_iotools.py
@@ -370,6 +370,12 @@ def str2bool(value):
raise ValueError("Invalid boolean")
+class ConverterError(Exception):
+ pass
+
+class ConverterLockError(ConverterError):
+ pass
+
class StringConverter:
"""
@@ -574,16 +580,39 @@ class StringConverter:
except ValueError:
# Raise an exception if we locked the converter...
if self._locked:
- raise ValueError("Converter is locked and cannot be upgraded")
+ errmsg = "Converter is locked and cannot be upgraded"
+ raise ConverterLockError(errmsg)
_statusmax = len(self._mapper)
# Complains if we try to upgrade by the maximum
if self._status == _statusmax:
- raise ValueError("Could not find a valid conversion function")
+ errmsg = "Could not find a valid conversion function"
+ raise ConverterError(errmsg)
elif self._status < _statusmax - 1:
self._status += 1
(self.type, self.func, self.default) = self._mapper[self._status]
self.upgrade(value)
- #
+
+ def iterupgrade(self, value):
+ self._checked = True
+ if not hasattr(value, '__iter__'):
+ value = (value,)
+ _strict_call = self._strict_call
+ try:
+ map(_strict_call, value)
+ except ValueError:
+ # Raise an exception if we locked the converter...
+ if self._locked:
+ errmsg = "Converter is locked and cannot be upgraded"
+ raise ConverterLockError(errmsg)
+ _statusmax = len(self._mapper)
+ # Complains if we try to upgrade by the maximum
+ if self._status == _statusmax:
+ raise ConverterError("Could not find a valid conversion function")
+ elif self._status < _statusmax - 1:
+ self._status += 1
+ (self.type, self.func, self.default) = self._mapper[self._status]
+ self.iterupgrade(value)
+
def update(self, func, default=None, missing_values='', locked=False):
"""
Set StringConverter attributes directly.
@@ -617,7 +646,7 @@ class StringConverter:
self.type = self._getsubdtype(default)
else:
try:
- tester = func('0')
+ tester = func('1')
except (TypeError, ValueError):
tester = None
self.type = self._getsubdtype(tester)