Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# sample of ast module
# reference:
import ast
import sys
import logging
class SampleVisitor(ast.NodeVisitor):
def generic_visit(self, node):
print("type of node: {0}".format(type(node).__name__))
ast.NodeVisitor.generic_visit(self, node)
def visit_Load(self, node):
def visit_Name(self, node):
print("Name: {0}".format(
def visit_Num(self, node):
print("Num: {0}".format(node.n))
if len(sys.argv) != 2:
logging.error("usage: [word]")
expr = sys.argv[1]
print("expression: {0}".format(expr))
tree = ast.parse(expr)
sv = SampleVisitor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.