/Lib/lib2to3/fixes/fix_itertools.py
Python | 41 lines | 30 code | 2 blank | 9 comment | 0 complexity | d2b48acbc9d415b64f3c71575fbfb9df MD5 | raw file
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 it_funcs = "('imap'|'ifilter'|'izip'|'ifilterfalse')" 16 PATTERN = """ 17 power< it='itertools' 18 trailer< 19 dot='.' func=%(it_funcs)s > trailer< '(' [any] ')' > > 20 | 21 power< func=%(it_funcs)s trailer< '(' [any] ')' > > 22 """ %(locals()) 23 24 # Needs to be run after fix_(map|zip|filter) 25 run_order = 6 26 27 def transform(self, node, results): 28 prefix = None 29 func = results['func'][0] 30 if 'it' in results and func.value != 'ifilterfalse': 31 dot, it = (results['dot'], results['it']) 32 # Remove the 'itertools' 33 prefix = it.get_prefix() 34 it.remove() 35 # Replace the node wich contains ('.', 'function') with the 36 # function (to be consistant with the second part of the pattern) 37 dot.remove() 38 func.parent.replace(func) 39 40 prefix = prefix or func.get_prefix() 41 func.replace(Name(func.value[1:], prefix=prefix))