1 | n/a | """ Fixer for itertools.(imap|ifilter|izip) --> (map|filter|zip) and |
---|
2 | n/a | itertools.ifilterfalse --> itertools.filterfalse (bugs 2360-2363) |
---|
3 | n/a | |
---|
4 | n/a | imports from itertools are fixed in fix_itertools_import.py |
---|
5 | n/a | |
---|
6 | n/a | If itertools is imported as something else (ie: import itertools as it; |
---|
7 | n/a | it.izip(spam, eggs)) method calls will not get fixed. |
---|
8 | n/a | """ |
---|
9 | n/a | |
---|
10 | n/a | # Local imports |
---|
11 | n/a | from .. import fixer_base |
---|
12 | n/a | from ..fixer_util import Name |
---|
13 | n/a | |
---|
14 | n/a | class FixItertools(fixer_base.BaseFix): |
---|
15 | n/a | BM_compatible = True |
---|
16 | n/a | it_funcs = "('imap'|'ifilter'|'izip'|'izip_longest'|'ifilterfalse')" |
---|
17 | n/a | PATTERN = """ |
---|
18 | n/a | power< it='itertools' |
---|
19 | n/a | trailer< |
---|
20 | n/a | dot='.' func=%(it_funcs)s > trailer< '(' [any] ')' > > |
---|
21 | n/a | | |
---|
22 | n/a | power< func=%(it_funcs)s trailer< '(' [any] ')' > > |
---|
23 | n/a | """ %(locals()) |
---|
24 | n/a | |
---|
25 | n/a | # Needs to be run after fix_(map|zip|filter) |
---|
26 | n/a | run_order = 6 |
---|
27 | n/a | |
---|
28 | n/a | def transform(self, node, results): |
---|
29 | n/a | prefix = None |
---|
30 | n/a | func = results['func'][0] |
---|
31 | n/a | if ('it' in results and |
---|
32 | n/a | func.value not in ('ifilterfalse', 'izip_longest')): |
---|
33 | n/a | dot, it = (results['dot'], results['it']) |
---|
34 | n/a | # Remove the 'itertools' |
---|
35 | n/a | prefix = it.prefix |
---|
36 | n/a | it.remove() |
---|
37 | n/a | # Replace the node which contains ('.', 'function') with the |
---|
38 | n/a | # function (to be consistent with the second part of the pattern) |
---|
39 | n/a | dot.remove() |
---|
40 | n/a | func.parent.replace(func) |
---|
41 | n/a | |
---|
42 | n/a | prefix = prefix or func.prefix |
---|
43 | n/a | func.replace(Name(func.value[1:], prefix=prefix)) |
---|