2to3: Apply `map` fixer. #3216

Merged
merged 1 commit into from Apr 11, 2013

Projects

None yet

2 participants

@charris
Member
charris commented Apr 9, 2013

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

@njsmith njsmith and 1 other commented on an outdated diff Apr 10, 2013
numpy/distutils/system_info.py
@@ -2077,7 +2077,7 @@ def combine_paths(*args, **kws):
if not args:
return []
if len(args) == 1:
- result = reduce(lambda a, b: a + b, map(glob, args[0]), [])
+ result = reduce(lambda a, b: a + b, [glob(_m) for _m in args[0]], [])
@njsmith
njsmith Apr 10, 2013 Member

Heh, this reduce is just a reimplementation of __builtins__.sum. (Not that this is really relevant to this PR.)

@charris
charris Apr 10, 2013 Member

Yep, and not the only instance. I'm thinking the the map version is actually a bit cleaner than the list, and reduce/sum both work fine with iterators. Hmm...

@njsmith njsmith commented on an outdated diff Apr 10, 2013
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))
+ >>> list(map(np.array, np.broadcast_arrays(x, y)))
@njsmith
njsmith Apr 10, 2013 Member

For docs I think I prefer recommending

>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
@njsmith
Member
njsmith commented Apr 10, 2013

Anyway this all looks generally fine to me, feel free to merge after resolving the above however you feel like.

@charris charris 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
a616479
@charris charris merged commit 25d55b8 into numpy:master Apr 11, 2013

1 check passed

default The Travis build passed
Details
@charris charris deleted the charris:2to3-apply-map-fixer branch Apr 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment