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*
    ```
  • 相关阅读:
    如何把pdf文档转化为word
    Win7系统中wmiprvse.exe占用CPU高如何解决
    Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
    SQL Server新增用户并控制访问权限设置。
    vs2013 中已经添加了引用,编译还是提示没有添加引用
    vue-cli中引入jquery方法
    vue-vuex安装
    vue2.0项目结构和打包发布
    从(0)新开始vue2.0【安装】
    js判断网络连接情况:navigator.onLine
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/7763843.html
Copyright © 2011-2022 走看看