==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:全局变量设置之value vs constant vs rootscope vs 服务[转]

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文要讲讲Angular中value vs. constant以及全局变量的设置 本教程 ...

  2. POSTGRESQL 锁表的问题

    一.找出所的语句 select wait.pid, wait.query as wait_query, wait.query_start as wait_query_start, wait.lockt ...

  3. filter中的DelegatingFilterProxy使用事例

    最近发现在filter内使用DelegatingFilterProxy过滤内容,那么为什么不用自带的Filter而使用Spring的DelegatingFilterProxy哪?最后才明白是因为fil ...

  4. stingray中的需要注意的地方

    TBLROWS中嵌套的单引号需要加倍用作转译

  5. 解决ODI 12C Studio 运行缓慢问题

    一.配置 ODI 12C Studio 1.1 修改ODI Studio process的-Xms和-Xmx ide.conf: modifying the initial Heap size (-X ...

  6. SpringBoot集成自定义HandlerMethodArgumentResolver

    传统SpringMVC集成自定义HandlerMethodArgumentResolver的方式为:http://www.cnblogs.com/yangzhilong/p/6282218.html ...

  7. NFS 网络文件系统测试笔记

    NFS(Network Files System),网络文件系统是1980年由SUN发展出来在UNIX&Linux系统间实现磁盘文件共享的一种方法.它是一种文件系统协议:支持应用程序在客户端通 ...

  8. CAP理论中, P(partition tolerance, 分区容错性)的合理解释

    在CAP理论中, 对partition tolerance分区容错性的解释一般指的是分布式网络中部分网络不可用时, 系统依然正常对外提供服务, 而传统的系统设计中往往将这个放在最后一位. 这篇文章对这 ...

  9. masonry瀑布流的使用

    今天在使用masonry.pkgd.min.js瀑布流的时候遇到一个很奇怪的问题,官网显示正常,而我的就是显示不正确,然后我又查看一遍,原来要加这段代码就ok了,记录一下,怕以后还会遇到这个问题 *, ...

  10. ios中文件下载(带缓存)

    使用asiHttPRequst框架 封装下载类 #import <Foundation/Foundation.h> #define FILESDOWNLOADCOMPLETE @" ...