rewriting code with ast; python

左心房为你撑大大i 提交于 2019-12-24 12:47:33

问题


I am learning AST and it seems like a powerful thing but I'm confused where the code went and why it disappeared. Say I want to rewrite

example = """def fake(x):\n
    y = ['useless list']\n
    return x
"""

as

example = """def fake(x):\n
    return x
"""

I can't see any way to rewrite in this manner. I can't even find a way to get the text of the line:

In [1]: example = """def fake(x):\n
   ...:     y = ['useless list']\n
   ...:     return x
   ...: """

In [3]: import ast

In [4]: p = ast.parse(example)

In [5]: p
Out[5]: <_ast.Module at 0x7f22f7274a10>

In [6]: p.body
Out[6]: [<_ast.FunctionDef at 0x7f22f7274a50>]

In [7]: p.body
Out[7]: [<_ast.FunctionDef at 0x7f22f7274a50>]

In [8]: f = p.body[0]

In [9]: f
Out[9]: <_ast.FunctionDef at 0x7f22f7274a50>

In [10]: f.body
Out[10]: [<_ast.Assign at 0x7f22f7274b10>, <_ast.Return at 0x7f22f7274c10>]

In [11]: f.name
Out[11]: 'fake'

In [12]: newf = f.body[1:]

In [13]: newf
Out[13]: [<_ast.Return at 0x7f22f7274c10>]

In [14]: z = newf[0]

In [15]: z.value
Out[15]: <_ast.Name at 0x7f22f7274c50>

In [16]: z.value.id
Out[16]: 'x'

Even more surprising is how it gives you the lineno of the start, but not the end. So you know where a function begins, but not where it ends, which is of no use

How can I grab the code and rewrite this func without the list y? Thank you


回答1:


Maybe this will help you:

import ast
import astor

example = """
def fake(x):
    y = ['useless list']
    return x
"""

tree = ast.parse(example)

# iterating through list which is represents function on ast
for ind, item in enumerate(tree.body[0].body):
    if isinstance(item, ast.Assign) and isinstance(item.value, ast.List):
        del tree.body[0].body[ind]
        break

print astor.to_source(tree)

Better using more OOP:

class RemoveList(ast.NodeTransformer):
    def visit_FunctionDef(self, node):
        self.generic_visit(node)
        for leaf in node.body:
            if isinstance(leaf, ast.Assign) and isinstance(leaf.value, ast.List):
                del node.body[node.body.index(leaf)]
        ast.fix_missing_locations(node)
        return node


tree_class = ast.parse(example)
remove_list = RemoveList().visit(tree_class)
print astor.to_source(tree_class)

Even better using RedBaron Full Syntax Tree:

from redbaron import RedBaron

example = """
def fake(x):
    y = ['useless list']
    return x
"""
red = RedBaron(example)
methods = red.find_all('DefNode').data
for data in methods:
    if len(data.value.data) > 0:
        for vals in data.value.data:
            if vals[0].type == 'assignment' and vals[0].value.type == 'list':
                index = vals[0].index_on_parent
                par = vals[0].parent
                del par[index]
                break

print red.dumps()



回答2:


There is now a library that can tell you the exact range of text for each node: ASTTokens. Here's your example:

import ast, asttokens
atok = asttokens.ASTTokens(example, parse=True)

for node in ast.walk(atok.tree):
  if hasattr(node, 'lineno'):
    print atok.get_text_range(node), node.__class__.__name__, atok.get_text(node)

This would produce this output (note the positions in the text):

(0, 50) FunctionDef def fake(x):
    y = ['useless list']
    return x
(17, 37) Assign y = ['useless list']
(42, 50) Return return x
(9, 10) Name x
(17, 18) Name y
(21, 37) List ['useless list']
(49, 50) Name x
(22, 36) Str 'useless list'


来源:https://stackoverflow.com/questions/36022935/rewriting-code-with-ast-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!