zoukankan      html  css  js  c++  java
  • 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 + "
    "
        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*
    ```
  • 相关阅读:
    MongoDB学习
    Linux 硬盘分区、分区、删除分区、格式化、挂载、卸载
    openstack中数据库连接数太多--pymysql.err.OperationalError,1040, u'Too many connections'
    openstack各服务端口使用情况
    linux常用命令
    云计算---OpenStack Neutron详解
    shell---数据流重定向
    云计算---openstack创建虚拟机过程
    SpringMvc面试题
    Linux 本机/异机文件对比
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/7763843.html
Copyright © 2011-2022 走看看