Skip to content

Commit

Permalink
Merge pull request numpy#15883 from eric-wieser/iotools-upgrade_helper
Browse files Browse the repository at this point in the history
MAINT: Remove duplicated code in iotools.py
  • Loading branch information
mattip authored Apr 3, 2020
2 parents 2ac14a1 + 3721469 commit 8c2112d
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions numpy/lib/_iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,26 @@ def __call__(self, value):
return self._callingfunction(value)
#

def _do_upgrade(self):
# 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
_status = self._status
if _status == _statusmax:
errmsg = "Could not find a valid conversion function"
raise ConverterError(errmsg)
elif _status < _statusmax - 1:
_status += 1
self.type, self.func, default = self._mapper[_status]
self._status = _status
if self._initial_default is not None:
self.default = self._initial_default
else:
self.default = default

def upgrade(self, value):
"""
Find the best converter for a given string, and return the result.
Expand All @@ -736,24 +756,7 @@ def upgrade(self, value):
try:
return self._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
_status = self._status
if _status == _statusmax:
errmsg = "Could not find a valid conversion function"
raise ConverterError(errmsg)
elif _status < _statusmax - 1:
_status += 1
(self.type, self.func, default) = self._mapper[_status]
self._status = _status
if self._initial_default is not None:
self.default = self._initial_default
else:
self.default = default
self._do_upgrade()
return self.upgrade(value)

def iterupgrade(self, value):
Expand All @@ -765,25 +768,7 @@ def iterupgrade(self, value):
for _m in value:
_strict_call(_m)
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
_status = self._status
if _status == _statusmax:
raise ConverterError(
"Could not find a valid conversion function"
)
elif _status < _statusmax - 1:
_status += 1
(self.type, self.func, default) = self._mapper[_status]
if self._initial_default is not None:
self.default = self._initial_default
else:
self.default = default
self._status = _status
self._do_upgrade()
self.iterupgrade(value)

def update(self, func, default=None, testing_value=None,
Expand Down

0 comments on commit 8c2112d

Please sign in to comment.