abstract-syntax-tree

Getting parent of AST node in Python

Getting parent of AST node in Python Question: I’m working with Abstract Syntax Trees in Python 3. The ast library gives many ways to get children of the node (you can use iter_child_nodes() or walk()) but no ways to get parent of one. Also, every node has links to its children, but it hasn’t links …

Total answers: 4

Why should json.loads be preferred to ast.literal_eval for parsing JSON?

Why should json.loads be preferred to ast.literal_eval for parsing JSON? Question: I have a dictionary that is stored in a db field as a string. I am trying to parse it into a dict, but json.loads gives me an error. Why does json.loads fail on this and ast.literal_eval works? Is one preferable over the other? …

Total answers: 7

Python AST – merging two ASTs

Python AST – merging two ASTs Question: Do you have any idea how can i merge two asts using python ast? I would like to do something like this: n1 = ast.parse(input_a) n2 = ast.parse(input_b) n = merge(n1,n2) I would like create root n with childs n1 and n2. Thanks in advance Asked By: Kapucko …

Total answers: 1

Using python's eval() vs. ast.literal_eval()

Using python's eval() vs. ast.literal_eval() Question: I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across plenty of information about the potential danger it can cause. That said, I’m very wary about using it. My situation …

Total answers: 7

Generate .pyc from Python AST?

Generate .pyc from Python AST? Question: How would I generate a .pyc file from a Python AST such that I could import the file from Python? I’ve used compile to create a code object, then written the co_code attribute to a file, but when I try to import the file from Python, I get an …

Total answers: 2

Python AST with preserved comments

Python AST with preserved comments Question: I can get AST without comments using import ast module = ast.parse(open(‘/path/to/module.py’).read()) Could you show an example of getting AST with preserved comments (and whitespace)? Asked By: Andrei || Source Answers: The ast module doesn’t include comments. The tokenize module can give you comments, but doesn’t provide other program …

Total answers: 7

python: get the abstract syntax tree of imported function?

python: get the abstract syntax tree of imported function? Question: Let’s say I’ve already imported a python module in the interpreter. How can I get the abstract syntax tree of the imported module (and any functions and classes within it) within the interpreter? I don’t want to have to re-parse the source files. Thanks! Asked …

Total answers: 2

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

How to write the Visitor Pattern for Abstract Syntax Tree in Python? Question: My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it? As far as I understand, each Node in AST would have visit() method (?) that would somehow get …

Total answers: 3

Simple example of how to use ast.NodeVisitor?

Simple example of how to use ast.NodeVisitor? Question: Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google. Asked By: lacker || Source Answers: generic_visit is …

Total answers: 3