I’m currently trying to find all ast.Import and ast.ImportFrom nodes in a Python file. However, in a multi-line import statement, like the lineno mentioned for each submodule (bar, baz, and coo) are all at the line where they are mentioned, not the original from foo import line. How can I get just the l…
Tag: abstract-syntax-tree
How do I use Python AST module to obtain all targets and value for assignment nodes?
Similar unanswered question: Python AST – How to see value of assign node? I am trying to obtain the assignments statements in a file. Example: and form them into a dictionary like so: I only want the Assigns with value and target, no call objects. The AST module is very difficult to understand and I wo…
How can I create an abstract syntax tree considering ‘|’? (Ply / Yacc)
Considering the following grammar: This is my code using ply: How am I supposed to build an AST with the expression if I can’t access the operators? I could separate the functions like p_expr_plus, but in this case, I would eliminate operator precedence. The docs are not so helpful, since I’m a be…
Using python’s eval() vs. ast.literal_eval()
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 is that I have input being given by a user…