def remove_simple_assignments(node):
"""
Remove one assigment of the form `<variable> = <variable>` at a time,
touching only the top level statements of the block.
"""
remaining_nodes = list(node.body)
new_nodes = []
while len(remaining_nodes) > 0:
cur_node = remaining_nodes.pop(0)
if type(cur_node) == ast.Assign:
can_remove, dest_name, src_name = _can_remove_assignment(cur_node, remaining_nodes)
if can_remove:
remaining_nodes = replace_name(
remaining_nodes, ctx=dict(dest_name=dest_name, src_name=src_name))
else:
new_nodes.append(cur_node)
else:
new_nodes.append(cur_node)
if len(new_nodes) == len(node.body):
return node
return replace_fields(node, body=new_nodes)
评论列表
文章目录