/Lib/lib2to3/fixes/fix_itertools.py

http://unladen-swallow.googlecode.com/ · Python · 41 lines · 23 code · 5 blank · 13 comment · 3 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. imports from itertools are fixed in fix_itertools_import.py
  4. If itertools is imported as something else (ie: import itertools as it;
  5. it.izip(spam, eggs)) method calls will not get fixed.
  6. """
  7. # Local imports
  8. from .. import fixer_base
  9. from ..fixer_util import Name
  10. class FixItertools(fixer_base.BaseFix):
  11. it_funcs = "('imap'|'ifilter'|'izip'|'ifilterfalse')"
  12. PATTERN = """
  13. power< it='itertools'
  14. trailer<
  15. dot='.' func=%(it_funcs)s > trailer< '(' [any] ')' > >
  16. |
  17. power< func=%(it_funcs)s trailer< '(' [any] ')' > >
  18. """ %(locals())
  19. # Needs to be run after fix_(map|zip|filter)
  20. run_order = 6
  21. def transform(self, node, results):
  22. prefix = None
  23. func = results['func'][0]
  24. if 'it' in results and func.value != 'ifilterfalse':
  25. dot, it = (results['dot'], results['it'])
  26. # Remove the 'itertools'
  27. prefix = it.get_prefix()
  28. it.remove()
  29. # Replace the node wich contains ('.', 'function') with the
  30. # function (to be consistant with the second part of the pattern)
  31. dot.remove()
  32. func.parent.replace(func)
  33. prefix = prefix or func.get_prefix()
  34. func.replace(Name(func.value[1:], prefix=prefix))