""" Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
from lib2to3 import fixer_base
from lib2to3.fixer_util import BlankLine, syms, token
class FixItertoolsImports(fixer_base.BaseFix):
import_from< 'from' 'itertools' 'import' imports=any >
def transform(self, node, results):
imports = results['imports']
if imports.type == syms.import_as_name or not imports.children:
children = imports.children
for child in children[::2]:
if child.type == token.NAME:
elif child.type == token.STAR:
# Just leave the import as is.
assert child.type == syms.import_as_name
name_node = child.children[0]
member_name = name_node.value
if member_name in ('imap', 'izip', 'ifilter'):
elif member_name in ('ifilterfalse', 'izip_longest'):
name_node.value = ('filterfalse' if member_name[1] == 'f'
# Make sure the import statement is still sane
children = imports.children[:] or [imports]
if remove_comma and child.type == token.COMMA:
while children and children[-1].type == token.COMMA:
# If there are no imports left, just get rid of the entire statement
if (not (imports.children or getattr(imports, 'value', None)) or