python抽象语法树

Abstract Syntax Tree is a very strong features in Python. Python AST module allows us to interact with Python code itself and modify it.

抽象语法树是Python中非常强大的功能。 Python AST模块允许我们与Python代码本身进行交互并对其进行修改。

Python AST模块 (Python AST Module)

With the Python AST module, we can do a lot of things like modifying Python code and inspect it. The code can be parsed and modified before it is compiled to bytecode form. It is important to understand that each Abstract Syntax Tree represents each element in our Python code as an object. We will understand this in detail in the coming sections. Let’s try the real code.

使用Python AST模块,我们可以做很多事情,例如修改Python代码并检查它。 在将代码编译为bytecode形式之前,可以对其进行解析和修改。 重要的是要理解,每个抽象语法树都将Python代码中的每个元素表示为一个对象。 我们将在接下来的部分中详细了解这一点。 让我们尝试真正的代码。

代码编译模式 (Modes for Code Compilation)

As we mentioned mode in the last script above, there are three modes in which Python code can be compiled. They are:

正如我们在上面的最后一个脚本中提到的mode一样,可以在三种模式下编译Python代码。 他们是:

  • exec: We can execute normal Python code using this mode.exec :我们可以使用这种模式执行普通的Python代码。
  • eval: To evaluate Python’s expressions, this mode will return the result fo the expression after evaluation.eval :要求值Python的表达式,此模式将在求值后返回表达式的结果。
  • single: This mode works just like Python shell which execute one statement at a time.single :此模式的工作方式类似于Python Shell,一次执行一个语句。

执行代码 (Executing code)

We can use AST module to execute Python code. Here is a sample program:

我们可以使用AST模块执行Python代码。 这是一个示例程序:

import astcode = ast.parse("print('Hello world!')")
print(code)exec(compile(code, filename="", mode="exec"))

Let’s see the output for this program:

As mentioned above, we used exec mode here.

让我们看一下该程序的输出:

如上所述,我们在这里使用了exec模式。

评估Python表达式 (Evaluating Python Expression)

Based on the second mode we mentioned above, AST can be used to evaluate a Python expression and get the response of the expression. Let’s look at a code snippet:

基于我们上面提到的第二种模式,AST可用于评估Python表达式并获取该表达式的响应。 让我们看一下代码片段:

import astexpression = '6 + 8'
code = ast.parse(expression, mode='eval')print(eval(compile(code, '', mode='eval')))

Let’s see the output for this program:

It is also possible to see the AST which was formed for the above expression, just add this line with above script:

让我们看一下该程序的输出:

也可以看到为上述表达式形成的AST,只需在上面的脚本中添加以下行即可:

print(ast.dump(code))

This is what it gives:

这就是它的作用:

构造多行AST (Constructing multi-line ASTs)

Till now, we made a single line ASTs and in the last example, we also saw how they look using the dump. Now, we will make a transform multi-line Python code to an AST. Here is a sample program:

到现在为止,我们只做了一行AST,在最后一个示例中,我们还看到了使用转储的外观。 现在,我们将多行Python代码转换为AST。 这是一个示例程序:

import asttree = ast.parse('''
fruits = ['grapes', 'mango']
name = 'peter'for fruit in fruits:print('{} likes {}'.format(name, fruit))
''')print(ast.dump(tree))

Let’s see the output for this program:

We can visit each node by modifying the script:

让我们看一下该程序的输出:

我们可以通过修改脚本来访问每个节点:

import astclass NodeVisitor(ast.NodeVisitor):def visit_Str(self, tree_node):print('{}'.format(tree_node.s))class NodeTransformer(ast.NodeTransformer):def visit_Str(self, tree_node):return ast.Str('String: ' + tree_node.s)tree_node = ast.parse('''
fruits = ['grapes', 'mango']
name = 'peter'for fruit in fruits:print('{} likes {}'.format(name, fruit))
''')NodeTransformer().visit(tree_node)
NodeVisitor().visit(tree_node)

Let’s see the output for this program:

The Visitor class we made above implement methods that are called for each AST nodes whereas with Transformer class, it first calls the corresponding method for node and finally replaces it with the return value of the method. We can execute the methods here by adding this line:

让我们看一下该程序的输出:

我们上面制作的Visitor 类实现了为每个AST节点调用的方法,而对于Transformer类,它首先为节点调用相应的方法,最后将其替换为该方法的返回值。 我们可以通过添加以下行来在此处执行方法:

tree_node = ast.fix_missing_locations(tree_node)
exec(compile(tree_node, '', 'exec'))

Now the output will be:

现在的输出将是:

何时使用Python AST模块? (When to use Python AST Module?)

Many automation testing tools, code coverage tools rely on the power of the Abstract Syntax Trees to parse the source code and find the possible flaws and errors in the code. Apart from this, ASTs are also used in:

许多自动化测试工具,代码覆盖率工具依靠抽象语法树的功能来解析源代码并查找代码中可能存在的缺陷和错误。 除此之外,AST还用于:

  • Making IDEs intelligent and making a feature everyone knows as intellisense.制作的IDE智能化和制作特点大家都知道智能感知
  • Tools like Pylint uses ASTs to perform static code analysis像Pylint这样的工具使用AST执行静态代码分析
  • Custom Python interpreters自定义Python解释器

结论 (Conclusion)

In this lesson, we studied the AST module which is used to evaluate and modify the Python’s code in your program.

在本课程中,我们研究了AST模块,该模块用于评估和修改程序中的Python代码。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/19243/python-ast-abstract-syntax-tree

python抽象语法树

python抽象语法树_Python AST –抽象语法树相关推荐

  1. python树代码_浅析AST抽象语法树及Python代码实现

    在计算机科学中,抽象语法树(abstract syntax tree或者缩写为AST),或者语法树(syntax tree),是源代码的抽象语法结构的树状表现形式,这里特指编程语言的源代码.树上的每个 ...

  2. 基于Python的JS逆向和AST抽象语法树

    一.关于js逆向的一些知识点总结 javascript-数据类型中,null,undefinded都表示没有东西 普通函数,匿名函数,构造函数,一般常见的是匿名函数 函数可以当成字符串看待,可以看成参 ...

  3. python 抽象语法树_Python 的抽象语法树(一)

    Python的抽象语法树(一) 之前三章的内容,我们分别谈及了语法分析中涉及到的文法定义.文法分析以及消除左递归等内容. 今天我们来谈谈语法分析的另一大部分内容:抽象语法树. 抽象语法树 简单来说,抽 ...

  4. python 抽象语法树_用python演示一个简单的AST(抽象语法树)

    假设对'a + 3 * b'进行解释,其中a=2,b=5 代码很简单,就不再进行详细的解释了. Num = lambda env, n: n Var = lambda env, x: env[x] A ...

  5. python ast_python AST 抽象语法树

    Abstract Sytax Tree 暂时用到的原因:在模型量化中,需要量化某些操作符带来的运算效果,比如 '+', '-','*', '/' 等等,这些就需要对源代码进行查询,因此就要需要将pyt ...

  6. js 数组 实现 完全树_JavaScript的工作原理:解析、抽象语法树(AST)+ 提升编译速度5个技巧

    摘要: JS的"编译原理". 原文:JavaScript的工作原理:解析.抽象语法树(AST)+ 提升编译速度5个技巧 作者:前端小智 Fundebug经授权转载,版权归原作者所有 ...

  7. html转换成抽象语法树,五分钟了解抽象语法树(AST)babel是如何转换的?

    抽象语法树 什么是抽象语法树? It is a hierarchical program representation that presents source code structure acco ...

  8. 超级详细AST抽象语法树Javascript

    AST 抽象语法树 Program 程序(开始) interface Program <: Node {type: "Program";body: [ Statement ] ...

  9. 详解AST抽象语法树

    浅谈 AST 先来看一下把一个简单的函数转换成AST之后的样子. // 简单函数 function square(n) {return n * n; }// 转换后的AST {type: " ...

最新文章

  1. 简评游戏人工智能相关的中文书(下)
  2. 部署exchange2010三合一:之七:安装证书服务器
  3. graylog2安装
  4. python手机版安卓-当python遇到Android手机 那么,万物皆可盘
  5. 树中两个结点的最低公共祖先
  6. 【编程6】贪吃蛇游戏(python+pygame)
  7. Nvidia GPU如何在Kubernetes 里工作
  8. 经纬度绘图_用编程赋能工作系列——百度VS高德经纬度互转
  9. C++OpenCV:三角形插值、线面的交点
  10. http 和 https 区别?
  11. 410. 分割数组的最大值
  12. Response.End() 与Response.Close()的区别
  13. 站立会议(11月23日
  14. HDU3364 Lanterns
  15. JSP编程中遇到的问题及解决
  16. 怎么复制黑苹果config配置_[黑苹果硬件] 实用黑苹果配置推荐
  17. 儿童机器人编程入门优先学习什么?
  18. 怎样在线将图片转换成icon图标
  19. linux hairpin mode
  20. ICANN认证注册商小全 英、德、法

热门文章

  1. 迅驰笔记本电脑怎样和TP-LINK无线路由器建立无线连接
  2. JSON数据转换总结(VIP典藏版)
  3. 文本分类在内容安全应用中的数据不平衡问题
  4. Jquery 获取当前时间日期
  5. 经过5个月的体验,我写下这篇抖音短视频的产品分析
  6. access to同义替换_常用高级词汇替换
  7. 美国国际救援小组:地震中自救窍门
  8. 新手入门,最佳练手三脚架推荐
  9. 信息论入门:信息守恒定律与纠错码
  10. 从零到一实现神经网络(一):感知机算法