==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 模块详解的更多相关文章

  1. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  2. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  5. python标准库介绍——5 re模块详解

    == re 模块== "Some people, when confronted with a problem, think 'I know, I'll use regular expres ...

  6. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  7. python标准库介绍——32 Queue 模块详解

    Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...

  8. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

  9. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

随机推荐

  1. angularJS 事件广播与接收[转]

    路由的事件 事件这个词在前端出现的频率真是高,根本拦不住,哪都是.$route服务在路由过程中的每个阶段都会触发不同的事件,可以为这些不同的路由事件设置监听器并做出响应. 一共有4个事件用来监听路由的 ...

  2. JAVA的CLASS文件详解

    一.事例 1.1 Test.java public class Test { public static void main(String[] args) { System.out.println(& ...

  3. Definitaion of 'utsname' must be imported from module 'Darwin.POSIX.sys.utsname' before it is required

    https://stackoverflow.com/questions/34430354/objective-c-gettimeofday-must-be-imported

  4. 〖Linux〗安装和使用virtualenv,方便多个Python版本中切换

    1. 安装pip easy_install pip 2. 安装virtualenvwrapper sudo pip install virtualenvwrapper 3. 使用virtualenv ...

  5. python模块之httplib(在py3中功能进一步强大,请详看文档)

    # -*- coding: utf-8 -*-#python 27#xiaodeng#python模块之httplib(在py3中功能进一步强大,请详看文档) import httplib#是较为底层 ...

  6. 转 Linux定时执行任务命令at和crontab

    本文介绍在Linux下的两种定时执行任务的方法:at命令,以及crontab服务. (1)at命令 假如我们只是想要让特定任务运行一次,那么,这时候就要用到at监控程序了. 设置at命令很简单,指示定 ...

  7. java常见数据结构整理

    java中容器类数据结构主要在java.util包中. java.util包中三个重要的接口及特点:List(列表).Set(保证集合中元素唯一).Map(维护多个key-value键值对,保证key ...

  8. 适配新路由3(D2)的LEDE/OpenWrt固件

    使用MediaTek系列的芯片方案 Y1(R6830): MT7620A + MT7612EN(5G 866M) + winbond 25Q128FVFG + winbond W971GG6KB-25 ...

  9. adjustResize和adjustPan的比较

    在下面的描述中,编辑框的maxLine都设定为10. 在信息列表界面中,编辑框在RelativeLayout中定义.编辑框上边(above)的列表组件的高度不会缩小为0,导致显示出现一点问题. 当信息 ...

  10. javascript基础 思维导图2

    来源于:http://www.cnblogs.com/xianyulaodi/p/5886128.html 1.javascript数据类型 2.javascript数组 3.javascript运算 ...