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