Skip to content

Commit a2d583a

Browse files
committed
9,24小节完成
1 parent 923a7ec commit a2d583a

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

source/c09/p24_parse_and_analyzing_python_source.rst

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
----------
66
问题
77
----------
8-
You want to write programs that parse and analyze Python source code.
8+
你想写解析并分析Python源代码的程序。
99

1010
|
1111
1212
----------
1313
解决方案
1414
----------
15-
Most programmers know that Python can evaluate or execute source code provided in
16-
the form of a string. For example:
15+
大部分程序员知道Python能够计算或执行字符串形式的源代码。例如:
1716

1817
.. code-block:: python
1918
@@ -33,8 +32,7 @@ the form of a string. For example:
3332
9
3433
>>>
3534
36-
However, the ast module can be used to compile Python source code into an abstract
37-
syntax tree (AST) that can be analyzed. For example:
35+
尽管如此,``ast`` 模块能被用来将Python源码编译成一个可被分析的抽象语法树(AST)。例如:
3836

3937
.. code-block:: python
4038
@@ -59,11 +57,9 @@ syntax tree (AST) that can be analyzed. For example:
5957
kwargs=None))], orelse=[])])"
6058
>>>
6159
62-
Analyzing the source tree requires a bit of study on your part, but it consists of a collection
63-
of AST nodes. The easiest way to work with these nodes is to define a visitor
64-
class that implements various visit_NodeName() methods where NodeName() matches
65-
the node of interest. Here is an example of such a class that records information about
66-
which names are loaded, stored, and deleted.
60+
分析源码树需要你自己更多的学习,它是由一系列AST节点组成的。
61+
分析这些节点最简单的方法就是定义一个访问者类,实现很多 ``visit_NodeName()`` 方法,
62+
``NodeName()`` 匹配那些你感兴趣的节点。下面是这样一个类,记录了哪些名字被加载、存储和删除的信息。
6763

6864
.. code-block:: python
6965
@@ -102,15 +98,15 @@ which names are loaded, stored, and deleted.
10298
print('Stored:', c.stored)
10399
print('Deleted:', c.deleted)
104100
105-
If you run this program, you’ll get output like this:
101+
如果你运行这个程序,你会得到下面这样的输出:
106102

107103
.. code-block:: python
108104
109105
Loaded: {'i', 'range', 'print'}
110106
Stored: {'i'}
111107
Deleted: {'i'}
112108
113-
Finally, ASTs can be compiled and executed using the compile() function. For example:
109+
最后,AST可以通过 ``compile()`` 函数来编译并执行。例如:
114110

115111
.. code-block:: python
116112
@@ -132,18 +128,14 @@ Finally, ASTs can be compiled and executed using the compile() function. For exa
132128
----------
133129
讨论
134130
----------
135-
The fact that you can analyze source code and get information from it could be the start
136-
of writing various code analysis, optimization, or verification tools. For instance, instead
137-
of just blindly passing some fragment of code into a function like exec(), you could
138-
turn it into an AST first and look at it in some detail to see what it’s doing. You could
139-
also write tools that look at the entire source code for a module and perform some sort
140-
of static analysis over it.
131+
当你能够分析源代码并从中获取信息的时候,你就能写很多代码分析、优化或验证工具了。
132+
例如,相比盲目的传递一些代码片段到类似 ``exec()`` 函数中,你可以先将它转换成一个AST,
133+
然后观察它的细节看它到底是怎样做的。
134+
你还可以写一些工具来查看某个模块的全部源码,并且在此基础上执行某些静态分析。
141135

142-
143-
It should be noted that it is also possible to rewrite the AST to represent new code if you
144-
really know what you’re doing. Here is an example of a decorator that lowers globally
145-
accessed names into the body of a function by reparsing the function body’s source code,
146-
rewriting the AST, and recreating the function’s code object:
136+
需要注意的是,如果你知道自己在干啥,你还能够重写AST来表示新的代码。
137+
下面是一个装饰器例子,可以通过重新解析函数体源码、
138+
重写AST并重新创建函数代码对象来将全局访问变量降为函数体作用范围,
147139

148140
.. code-block:: python
149141
@@ -198,7 +190,7 @@ rewriting the AST, and recreating the function’s code object:
198190
return func
199191
return lower
200192
201-
To use this code, you would write code such as the following:
193+
为了使用这个代码,你可以像下面这样写:
202194

203195
.. code-block:: python
204196
@@ -208,7 +200,7 @@ To use this code, you would write code such as the following:
208200
while n > 0:
209201
n -= INCR
210202
211-
The decorator rewrites the source code of the countdown() function to look like this:
203+
装饰器会将 ``countdown()`` 函数重写为类似下面这样子:
212204

213205
.. code-block:: python
214206
@@ -218,16 +210,11 @@ The decorator rewrites the source code of the countdown() function to look like
218210
while n > 0:
219211
n -= INCR
220212
221-
In a performance test, it makes the function run about 20% faster.
222-
223-
224-
Now, should you go applying this decorator to all of your functions? Probably not.
225-
However, it’s a good illustration of some very advanced things that might be possible
226-
through AST manipulation, source code manipulation, and other techniques.
227-
213+
在性能测试中,它会让函数运行快20%
228214

229-
This recipe was inspired by a similar recipe at ActiveState that worked by manipulating
230-
Python’s byte code. Working with the AST is a higher-level approach that might be a bit
231-
more straightforward. See the next recipe for more information about byte code.
215+
现在,你是不是想为你所有的函数都加上这个装饰器呢?或许不会。
216+
但是,这却是对于一些高级技术比如AST操作、源码操作等等的一个很好的演示说明
232217

218+
本节受另外一个在 ``ActiveState`` 中处理Python字节码的章节的启示。
219+
使用AST是一个更加高级点的技术,并且也更简单些。参考下面一节获得字节码的更多信息。
233220

0 commit comments

Comments
 (0)