ast.parse()

ast.parse(source, filename='', mode='exec') Parse the source into an AST node. Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST).

ast.NodeVisitor.visit()

visit(node) Visit a node. The default implementation calls the method called self.visit_classname where classname is the name of the node class, or generic_visit() if that method doesn’t exist.

ast.NodeVisitor.generic_visit()

generic_visit(node) This visitor calls visit() on all children of the node. Note that child nodes of nodes that have a custom visitor method won’t be visited unless the visitor calls generic_visit() or visits them itself.

ast.NodeVisitor

class ast.NodeVisitor 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. visit(node) Visit a node. The default implementation calls the method called self.visit_classname where classname is the name of the node class, or generic_visit() if that method doesn’t exist. gener

ast.NodeTransformer

class ast.NodeTransformer A NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes. The NodeTransformer will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is None, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place. Here is an example transforme

ast.literal_eval()

ast.literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None. This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions

ast.iter_fields()

ast.iter_fields(node) Yield a tuple of (fieldname, value) for each field in node._fields that is present on node.

ast.iter_child_nodes()

ast.iter_child_nodes(node) Yield all direct child nodes of node, that is, all fields that are nodes and all items of fields that are lists of nodes.

ast.increment_lineno()

ast.increment_lineno(node, n=1) Increment the line number of each node in the tree starting at node by n. This is useful to “move code” to a different location in a file.

ast.get_docstring()

ast.get_docstring(node, clean=True) Return the docstring of the given node (which must be a FunctionDef, ClassDef or Module node), or None if it has no docstring. If clean is true, clean up the docstring’s indentation with inspect.cleandoc().