abstract-syntax-tree

Ast literal_eval() gives leading zeroes error when reading string containing dictionary

Ast literal_eval() gives leading zeroes error when reading string containing dictionary Question: I am trying to use literal_eval() to read a string containing a dictionary that I am scraping from a site using requests and beautifulsoup. Need it to be in a dictionary so I can access the data more easily. Most cases have worked …

Total answers: 1

Subgraph isomorphism in NetworkX graphs from python ASTs

Subgraph isomorphism in NetworkX graphs from python ASTs Question: I have two python files. I generated two NetworkX graphs just traversing the ASTs of the files. I want to check – is there a sub-graph of one graph isomorphic to another one? Python files are presented below – whiletest.py a= 10 while(a <= 0): if …

Total answers: 1

Find number of lines in python functions without running it

Find number of lines in python functions without running it Question: I am trying to create a tool that identifies the number of lines of code in each function in a given python file. However- I do not want to actually run the code which is required with something like the inspect library suggested in …

Total answers: 3

Find the first line in a multi-line import from statement with Python's AST

Find the first line in a multi-line import from statement with Python's AST Question: 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 from foo import ( bar, baz, coo ) the lineno mentioned for each submodule (bar, baz, and coo) are all …

Total answers: 1

Python library for parsing code of any language into an AST?

Python library for parsing code of any language into an AST? Question: I’m looking for a Python library for parsing code into its abstract syntax tree representation. There exists a built-in module, named ast, however, it is only designed for parsing Python code, to my understanding. I’m wondering if there is a similar Python library …

Total answers: 2

Generating a text representation of Python's AST

Generating a text representation of Python's AST Question: With Clang we can do: clang -cc1 -ast-dump j.c TranslationUnitDecl 0x7fbcfc00f608 <<invalid sloc>> <invalid sloc> |-TypedefDecl 0x7fbcfc00fea0 <<invalid sloc>> <invalid sloc> implicit __int128_t ‘__int128’ | `-BuiltinType 0x7fbcfc00fba0 ‘__int128’ |-TypedefDecl 0x7fbcfc00ff08 <<invalid sloc>> <invalid sloc> implicit __uint128_t ‘unsigned __int128’ | `-BuiltinType 0x7fbcfc00fbc0 ‘unsigned __int128’ |-TypedefDecl 0x7fbcfc0101b8 <<invalid sloc>> …

Total answers: 2

How to rename all variables according to rule set with AST

How to rename all variables according to rule set with AST Question: Is there a tool that is capable of iterating over all variables and function arguments in a Python file and replace them according to a style rule? For example, given a function in myModule.py def myFunc(Variable): Tmp = sin(Variable) return Tmp * 2 …

Total answers: 1

How to find out if (the source code of) a function contains a loop?

How to find out if (the source code of) a function contains a loop? Question: Let’s say, I have a bunch of functions a, b, c, d and e and I want to find out if they directly use a loop: def a(): for i in range(3): print(i**2) def b(): i = 0 while i …

Total answers: 3

What does `return +/- ` do in python?

What does `return +/- ` do in python? Question: I was going through the CPython source code and I found the following piece of code from the standard library(ast.py). if isinstance(node.op, UAdd): return + operand else: return – operand I tried the following in my python interpreter >>> def s(): … return + 1 … …

Total answers: 4

using ast module to transform random constants in Python source

using ast module to transform random constants in Python source Question: I’m interested in writing a program that uses Python’s built-in AST module to randomly modify constants in arbitrary Python source. This transformation will likely involve a traversal of the abstract syntax tree representation using operations defined by the AST module. The module offers two …

Total answers: 1