ast
使用什么类型的树遍历(具体为ast.NodeVisitor()
)?当我创建一个堆栈并推送遍历到堆栈中的每个节点时,结果似乎是第一个'广度优先'树遍历。意味着订单依赖于树中的级别。
实施例。树看起来像
Module
Assign
Name
Store
Call
Attribute
Str
Load
并且堆栈看起来像
[Module,Assign,Name,Call,Store,Attribute,Str,Load]
实施例。代码
stack = []
class a(ast.NodeTransformer):
def visit_Num(self,node):
stack.append(node)
...
return node
... #this is all the other visit_*() functions
def visit_Str(self,node):
stack.append(node)
...
return node
if __name__ == "__main__":
with open('some_file.py','r') as pt:
tree = ast.parse(pt)
new_tree = a()
new_tree_edit = ast.fix_missing_locations(new_tree.visit(tree)) # I have tried with and without calling fix_missing_locations and got the same results.
print stack
答案 0 :(得分:3)
ast.walk()
函数首先使树呼吸;请参阅ast.py
source:
def walk(node):
"""
Recursively yield all descendant nodes in the tree starting at *node*
(including *node* itself), in no specified order. This is useful if you
only want to modify nodes in place and don't care about the context.
"""
from collections import deque
todo = deque([node])
while todo:
node = todo.popleft()
todo.extend(iter_child_nodes(node))
yield node
新节点被推入队列,正在遍历的下一个节点是队列的前端。
如果您想要深度优先遍历,请使用ast.NodeVisitor()
的子类;它会使用递归来走树; NodeVisitor.visit()
调用NodeVisitor.generic_visit()
,除非定义了更多特定于节点的访问者方法,NodeVisitor.generic_visit()
再次为子节点调用NodeVisitor.visit()
:
class NodeVisitor(object):
"""
A node visitor base class that walks the abstract syntax tree and calls a
visitor function for every node found. This function may return a value
which is forwarded by the `visit` method.
This class is meant to be subclassed, with the subclass adding visitor
methods.
Per default the visitor functions for the nodes are ``'visit_'`` +
class name of the node. So a `TryFinally` node visit function would
be `visit_TryFinally`. This behavior can be changed by overriding
the `visit` method. If no visitor function exists for a node
(return value `None`) the `generic_visit` visitor is used instead.
Don't use the `NodeVisitor` if you want to apply changes to nodes during
traversing. For this a special visitor exists (`NodeTransformer`) that
allows modifications.
"""
def visit(self, node):
"""Visit a node."""
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
"""Called if no explicit visitor function exists for a node."""
for field, value in iter_fields(node):
if isinstance(value, list):
for item in value:
if isinstance(item, AST):
self.visit(item)
elif isinstance(value, AST):
self.visit(value)
如果你做了NodeVisitor
的子类(或者它的衍生版本NodeTransformer
),请务必在特定的super(YourClass, self).generic_visit(node)
方法中调用visit_*
以继续穿过树。