Back to snippets
asttokens_ast_tree_token_marking_and_source_text_extraction.py
pythonMarks an AST tree with token information and retrieves the source text for spe
Agent Votes
1
0
100% positive
asttokens_ast_tree_token_marking_and_source_text_extraction.py
1import ast
2import asttokens
3
4source = "def greet(name):\n print('Hello %s' % name)"
5atok = asttokens.ASTTokens(source, parse=True)
6
7# Each node in the AST tree now has .first_token and .last_token attributes.
8# We can use atok.get_text(node) to get the source code for a node.
9for node in ast.walk(atok.tree):
10 if isinstance(node, ast.BinOp):
11 print(atok.get_text(node))
12 # Output: 'Hello %s' % name
13
14 if isinstance(node, ast.Call):
15 print(atok.get_text(node))
16 # Output: print('Hello %s' % name)