python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证你传递的是一个完成的 Python 语句. 在 [Example 2-47 #eg-2-47] 中, 我们一行一行地编译一个程序, 编译完成后会执行所得到的代码对象
(code object). 程序代码如下: ```
a = (
1,
2,
3
)
print a
``` 注意只有我们到达第 2 个括号, 元组的赋值操作能编译完成. ====Example 2-47. 使用 code 模块编译语句====[eg-2-47] ```
File: code-example-1.py import code
import string #
SCRIPT = [
"a = (",
" 1,",
" 2,",
" 3 ",
")",
"print a"
] script = "" for line in SCRIPT:
script = script + line + "\n"
co = code.compile_command(script, "<stdin>", "exec")
if co:
# got a complete statement. execute it!
print "-"*40
print script,
print "-"*40
exec co
script = "" *B*----------------------------------------
a = (
1,
2,
3
)
----------------------------------------
----------------------------------------
print a
----------------------------------------
(1, 2, 3)*b*
``` //InteractiveConsole// 类实现了一个交互控制台, 类似你启动的 Python 解释器交互模式. 控制台可以是活动的(自动调用函数到达下一行) 或是被动的(当有新数据时调用 //push// 方法).
默认使用内建的 ``raw_input`` 函数. 如果你想使用另个输入函数,
你可以使用相同的名称重载这个方法. [Example 2-48 #eg-2-48] 展示了如何使用 ``code``
模块来模拟交互解释器. ====Example 2-48. 使用 code 模块模拟交互解释器====[eg-2-48] ```
File: code-example-2.py import code console = code.InteractiveConsole()
console.interact() *B*Python 1.5.2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
(InteractiveConsole)
>>> a = (
... 1,
... 2,
... 3
... )
>>> print a
(1, 2, 3)*b*
``` [Example 2-49 #eg-2-49] 中的脚本定义了一个 ``keyboard`` 函数.
它允许你在程序中手动控制交互解释器. ====Example 2-49. 使用 code 模块实现简单的 Debugging====[eg-2-49] ```
File: code-example-3.py def keyboard(banner=None):
import code, sys # use exception trick to pick up the current frame
try:
raise None
except:
frame = sys.exc_info()[2].tb_frame.f_back # evaluate commands in current namespace
namespace = frame.f_globals.copy()
namespace.update(frame.f_locals) code.interact(banner=banner, local=namespace) def func():
print "START"
a = 10
keyboard()
print "END" func() *B*START
Python 1.5.2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
(InteractiveConsole)
>>> print a
10
>>> print keyboard
<function keyboard at 9032c8>
^Z
END*b*
```
python标准库介绍——30 code 模块详解的更多相关文章
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——5 re模块详解
== re 模块== "Some people, when confronted with a problem, think 'I know, I'll use regular expres ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——32 Queue 模块详解
Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
随机推荐
- 浅谈关于QT中Webkit内核浏览器
关于QT中Webkit内核浏览器是本文要介绍的内容,主要是来学习QT中webkit中浏览器的使用.提起WebKit,大家自然而然地想到浏览器.作为浏览器内部的主要构件,WebKit的主要工作是渲染.给 ...
- [Practical.Vim(2012.9)].Drew.Neil.Tip21学习摘要
Vim has three kinds of Visual mode. In character-wise Visual mode, we can select anything from a sin ...
- 〖Android〗Nexus 7 flo (razor) 刷入Recovery/CM-11.0后卡在开机动画的解决方法
操作日志: 起因:Nexus 7 flo 华硕平板,刷了一个CWM Recovery之后变成了砖机: 尝试1:使用CWM Recovery清除数据,开机失败 尝试2:刷入CM 11 Snapshot的 ...
- SpringCloud stream连接RabbitMQ收发信息
百度上查的大部分都是一些很简单的单消费者或者单生产者的例子,并且多是同一个服务器的配置,本文的例子为多服务器配置下的消费生产和消费者配置. 参考资料:https://docs.spring.io/sp ...
- 阿里云安装jdk,tomcat,maven,svn,git,nginx
1. 首先通过xftp等工具上传安装包 2. 配置目录 cd usr mkdir java cd java mkdir jdk mkdir tomcatmkdir maven 3. 安装jdk 3.1 ...
- ios中VRGCalendarView日历控件
http://pan.baidu.com/share/link?shareid=4166002480&uk=923776187 官网 https://github.com/TjeerdVuri ...
- mac安装thrift
一.安装brew包管理工具 不想Ubuntu自带apt,redhat自带yum,mac是不自带包管理工具的.需要自己安装,最常用的是brew,Homebrew简称brew,OSX上的软件包管理工具,在 ...
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- Category在项目中的实际运用
先看两行代码:1. label2.textColor = [UIColor colorWithHexString:@"707070"]; 2. _table.header = [M ...
- Oracle中INTERSECT,MINUS,UNION,UNION ALL用法
intersect 就是交集minus 就是差集 交集就是两个结果集中都有的元素 比如 select uid from tb1 intersect select uid from tb2 那么既存在t ...