Python AST ModuleIn this tutorial, we will learn how to use the AST to understand the code. What is AST Module?AST stands for Abstract Syntax Tree, which is a potent tool of the Python programming language. It allows us to interact with the Python code itself and can modify it. Have you ever thought about how the Python code gets run? Is there magic behind it? For those who don’t know, the Python interpreter is responsible for running the Python code. It follows the pre-written instructions that translate the Python code into instructions that a machine can run. Following is the process of converting a Python code into machine code.
The above description is a rough sketch of the how interpreter runs the Python code using the AST. Mode for Code CompilationThere are three modes available to compile the code. They are given below.
Executing Python CodeUsing the AST module, we can run the Python code. Let’s understand the following example. Example – Output: <_ast.Module object at 0x0000010B889A6AC0> Hello Learner! Welcome to JavaTpoint Evaluate Python ExpressionThe AST module allows us to evaluate the Python expression and return the result from the expression. Let’s understand the following example. Example – Output: 14 Expression(body=BinOp(left=Constant(value=6, kind=None), op=Add(), right=Constant(value=8, kind=None))) Creating Multi-line ASTsIn the previous example, we have seen the single line AST and how to dump them. Now, we will learn how we can create multi-line AST. First, let’s understand the following example. Example – Output: Module(body=[Assign(targets=[Name(id='subjects', ctx=Store())], value=List(elts=[Constant(value='computer science', kind=None), Constant(value='alorithm', kind=None)], ctx=Load()), type_comment=None), Assign(targets=[Name(id='name', ctx=Store())], value=Constant(value='Ricky', kind=None), type_comment=None), For(target=Name(id='fruit', ctx=Store()), iter=Name(id='fruits', ctx=Load()), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Call(func=Attribute(value=Constant(value='{} learn {}', kind=None), attr='format', ctx=Load()), args=[Name(id='name', ctx=Load()), Name(id='subjects', ctx=Load())], keywords=[])], keywords=[]))], orelse=[], type_comment=None)], type_ignores=[]) NodeTransformer and NodeVisitorThe NodeTransformer class is used to take different types and modify according to our requirement. The ast module also provides the NodeVisitor class, which helps us call the visit function every time we go through the tree. So that we can get more control on the nodes, let’s understand the following example. Example – 1 Output: Welcome to the Javatpoint Explanation – In the above code, we have imported the ast module that parses the code. Then we have defined the Visitor class that inherits the NodeVisitor class. Every time finds the string node; it gets transformed adding that prefix. We can also use the module when we run directly run the source code. Let’s understand the following example. Example – 2: Output: {'from': ['pprint'], 'import': ['ast']} Explanation – The above code converts the Python file into an abstract syntax tree. Then we analyze the tree to get helpful information. We have opened a Python file in the read mode and then create AST named ast_tree. Then, the parse() function processed all the tokens, followed all the language rules, and build a tree data structure that consists of much useful information. The tree is nothing but a collection of nodes, where a tree variable is referenced to the “root” node. Thus, we can visit each node in the tree and perform operations. But, first, we visit each node and processing the data. Analyze the ASTOnce we get the tree, now the Analyzer follows the visitor pattern. Using the NodeVisitor class, we can track any node in Python. We need to implement a method visit_<node type> to visit a particular node type. In the previous example, we used the below script. Example – The code accepts the name of the module and stores it in a list of statistics. With the help of NodeVisitor class, we can analyze the tree. The visit() method will work same as the visit_<node type> method. Using AST as Analysis ToolAfter the Python code turned into byte code, it can’t be readable by humans. But it makes the interpreter fast, which means byte code is designed for the machine, not for humans. AST consists of enough structured information, which makes them helpful in learning about Python code. However, ASTs are still not user-friendly, but they are much understandable than byte code representation. When to use Python AST module?Abstract Syntax Tree is quite helpful for code coverage tools. It parses the source code and finds the possible flaws and errors in the code. It can also use in –
ConclusionWe have learned about the ast module in Python, which is responsible for running the Python code. Then, we have built the AST tree from the Python code and perform analysis on the AST using a NodeVisitor class. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263498.html