diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-04-09 12:44:24 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-04-10 16:41:13 -0600 |
commit | a6164794a63215e23fa28432d9acec4727c68d02 (patch) | |
tree | e1f1cb2da6aac69cb3410b623fcf224ec433d1a4 /numpy/lib | |
parent | f85bdf48aadf7b5a5f575370b589805fed190a6c (diff) | |
download | numpy-a6164794a63215e23fa28432d9acec4727c68d02.tar.gz |
2to3: Apply `map` fixer.
In Python 3 `map` is an iterator while in Python 2 it returns a list.
The simple fix applied by the fixer is to inclose all instances of map
with `list(...)`. This is not needed in all cases, and even where
appropriate list comprehensions may be preferred for their clarity.
Consequently, this patch attempts to use list comprehensions where it
makes sense.
When the mapped function has two arguments there is another problem that
can arise. In Python 3 map stops execution when the shortest argument
list is exhausted, while in Python 2 it stops when the longest argument
list is exhausted. Consequently the two argument case might need special
care. However, we have been running Python3 converted versions of numpy
since 1.5 without problems, so it is probably not something that affects
us.
Closes #3068
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/_iotools.py | 3 | ||||
-rw-r--r-- | numpy/lib/financial.py | 10 | ||||
-rw-r--r-- | numpy/lib/index_tricks.py | 4 | ||||
-rw-r--r-- | numpy/lib/npyio.py | 21 | ||||
-rw-r--r-- | numpy/lib/recfunctions.py | 2 | ||||
-rw-r--r-- | numpy/lib/shape_base.py | 2 | ||||
-rw-r--r-- | numpy/lib/stride_tricks.py | 4 |
7 files changed, 24 insertions, 22 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index 34c85e47e..9eaf4d78f 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -721,7 +721,8 @@ class StringConverter(object): value = (value,) _strict_call = self._strict_call try: - map(_strict_call, value) + for _m in value: + _strict_call(_m) except ValueError: # Raise an exception if we locked the converter... if self._locked: diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index 28e63f52b..cd21db0f2 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -115,7 +115,7 @@ def fv(rate, nper, pmt, pv, when='end'): """ when = _convert_when(when) - rate, nper, pmt, pv, when = map(np.asarray, [rate, nper, pmt, pv, when]) + (rate, nper, pmt, pv, when) = map(np.asarray, [rate, nper, pmt, pv, when]) temp = (1+rate)**nper miter = np.broadcast(rate, nper, pmt, pv, when) zer = np.zeros(miter.shape) @@ -206,7 +206,7 @@ def pmt(rate, nper, pv, fv=0, when='end'): """ when = _convert_when(when) - rate, nper, pv, fv, when = map(np.asarray, [rate, nper, pv, fv, when]) + (rate, nper, pv, fv, when) = map(np.asarray, [rate, nper, pv, fv, when]) temp = (1+rate)**nper miter = np.broadcast(rate, nper, pv, fv, when) zer = np.zeros(miter.shape) @@ -263,7 +263,7 @@ def nper(rate, pmt, pv, fv=0, when='end'): """ when = _convert_when(when) - rate, pmt, pv, fv, when = map(np.asarray, [rate, pmt, pv, fv, when]) + (rate, pmt, pv, fv, when) = map(np.asarray, [rate, pmt, pv, fv, when]) use_zero_rate = False old_err = np.seterr(divide="raise") @@ -502,7 +502,7 @@ def pv(rate, nper, pmt, fv=0.0, when='end'): """ when = _convert_when(when) - rate, nper, pmt, fv, when = map(np.asarray, [rate, nper, pmt, fv, when]) + (rate, nper, pmt, fv, when) = map(np.asarray, [rate, nper, pmt, fv, when]) temp = (1+rate)**nper miter = np.broadcast(rate, nper, pmt, fv, when) zer = np.zeros(miter.shape) @@ -568,7 +568,7 @@ def rate(nper, pmt, pv, fv, when='end', guess=0.10, tol=1e-6, maxiter=100): """ when = _convert_when(when) - nper, pmt, pv, fv, when = map(np.asarray, [nper, pmt, pv, fv, when]) + (nper, pmt, pv, fv, when) = map(np.asarray, [nper, pmt, pv, fv, when]) rn = guess iter = 0 close = False diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 41a891a39..b04e348b8 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -163,8 +163,8 @@ class nd_grid(object): isinstance(key[k].stop, float): typ = float if self.sparse: - nn = map(lambda x,t: _nx.arange(x, dtype=t), size, \ - (typ,)*len(size)) + nn = [_nx.arange(_x, dtype=_t) + for _x, _t in zip(size, (typ,)*len(size))] else: nn = _nx.indices(size, typ) for k in range(len(size)): diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 7f66fb141..600ab4a36 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1602,7 +1602,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): - current_column = map(itemgetter(i), rows) + current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: @@ -1653,18 +1653,19 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... -# if loose: -# conversionfuncs = [conv._loose_call for conv in converters] -# else: -# conversionfuncs = [conv._strict_call for conv in converters] -# for (i, vals) in enumerate(rows): -# rows[i] = tuple([convert(val) -# for (convert, val) in zip(conversionfuncs, vals)]) + # + # if loose: + # conversionfuncs = [conv._loose_call for conv in converters] + # else: + # conversionfuncs = [conv._strict_call for conv in converters] + # for (i, vals) in enumerate(rows): + # rows[i] = tuple([convert(val) + # for (convert, val) in zip(conversionfuncs, vals)]) if loose: - rows = zip(*[map(converter._loose_call, map(itemgetter(i), rows)) + rows = zip(*[[converter._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)]) else: - rows = zip(*[map(converter._strict_call, map(itemgetter(i), rows)) + rows = zip(*[[converter._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)]) # Reset the dtype data = rows diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index a9f480f5d..f909a4838 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -401,7 +401,7 @@ def merge_arrays(seqarrays, seqarrays = (seqarrays,) else: # Make sure we have arrays in the input sequence - seqarrays = map(np.asanyarray, seqarrays) + seqarrays = [np.asanyarray(_m) for _m in seqarrays] # Find the sizes of the inputs and their maximum sizes = tuple(a.size for a in seqarrays) maxlength = max(sizes) diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 631cf6820..de8606167 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -345,7 +345,7 @@ def dstack(tup): [[3, 4]]]) """ - return _nx.concatenate(map(atleast_3d,tup),2) + return _nx.concatenate([atleast_3d(_m) for _m in tup], 2) def _replace_zero_by_x_arrays(sub_arys): for i in range(len(sub_arys)): diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index 5c9d222f3..1f08131ec 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -60,7 +60,7 @@ def broadcast_arrays(*args): Here is a useful idiom for getting contiguous copies instead of non-contiguous views. - >>> map(np.array, np.broadcast_arrays(x, y)) + >>> [np.array(a) for a in np.broadcast_arrays(x, y)] [array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]), array([[1, 1, 1], @@ -68,7 +68,7 @@ def broadcast_arrays(*args): [3, 3, 3]])] """ - args = map(np.asarray, args) + args = [np.asarray(_m) for _m in args] shapes = [x.shape for x in args] if len(set(shapes)) == 1: # Common case where nothing needs to be broadcasted. |