The AST
module allows us to interact with Python code and modify it. The module is used by automation testing tools and code coverage tools to parse source code and find possible flaws and errors in the code.
The AST
module library in Python provides a tree structure for the source code. In Python, AST is compiled into a code object using the built-in compile()
function.
The following diagram shows how Python converts a simple statement to its syntax tree.
For each line of code, create an AST node and then compare the value, node type, and other parameters like operator, operand, function name, class name, index, etc.
import astcode = ast.parse("6 * 7 - 3")print(ast.dump(code))