python标准库-builtin 模块之compile,execfile

eval函数仅仅允许执行简单的表达式。对于更大的代码块时,使用compileexec函数。

例子:使用 compile函数验证语法

NAME = "script.py"

BODY = """
prnt 'owl-stretching time'
"""

try:
    compile(BODY, NAME, "exec")
except SyntaxError, v:
    print "syntax error:", v, "in", NAME

结果如下:

syntax error: invalid syntax in script.py

当成功时,compile函数返回一个代码对象,你能够使用exec来执行他。

BODY = """
print 'the ant, an introduction'
"""

code = compile(BODY, "<script>", "exec")

print code

exec code

结果如下:

<code object ? at 8c6be0, file "<script>", line 0>
the ant, an introduction

为了能够快速的生成代码,你可以使用下面例子中的类。使用write方法来添加状态,indent和**dedent **来添加结构,这个类能够方便的让其他类进行调用。

import sys, string

class CodeGeneratorBackend:
    "Simple code generator for Python"

    def begin(self, tab="\t"):
        self.code = []
        self.tab = tab
        self.level = 0

    def end(self):
        self.code.append("") # make sure there's a newline at the end
        return compile(string.join(self.code, "\n"), "<code>", "exec")

    def write(self, string):
        self.code.append(self.tab * self.level + string)

    def indent(self):
        self.level += 1
        # in Python 1.5.2 and earlier, use this instead:
        # self.level = self.level + 1

    def dedent(self):
        if self.level == 0:
            raise SyntaxError, "internal error in code generator"
        self.level -= 1
        # in Python 1.5.2 and earlier, use this instead:
        # self.level = self.level - 1

#
# try it out!

c = CodeGeneratorBackend()
c.begin()
c.write("for i in range(5):")
c.indent()
c.write("print 'code generation made easy!'")
c.dedent()
exec c.end()

结果如下:

code generation made easy!
code generation made easy!
code generation made easy!
code generation made easy!
code generation made easy!

python也提供了execfile的函数,他能够方便的从文件中加载代码,并编译和执行。如下的例子是如何使用这个函数。

execfile("hello.py")

def EXECFILE(filename, locals=None, globals=None):
    exec compile(open(filename).read(), filename, "exec") in locals, globals

EXECFILE("hello.py")

结果如下:

hello again, and welcome to the show
hello again, and welcome to the show

在这个例子中 hello.py的代码如下:

print "hello again, and welcome to the show"

从__builtin__ 的模块中重载函数。

因为python是在验证局部和模块名称空间后才进行查看内建函数,在这种情况下你需要特别的引用__builtin__ 模块。在如下的例子脚本中,重载open一个版本函数,这个版本中能够打开一个普通文件验证是不是以一个“特殊”的字符串开始。为了能够使用原始的open函数,需要明确的引用模块名称。

def open(filename, mode="rb"):
    import __builtin__
    file = __builtin__.open(filename, mode)
    if file.read(5) not in("GIF87", "GIF89"):
        raise IOError, "not a GIF file"
    file.seek(0)
    return file

fp = open("samples/sample.gif")
print len(fp.read()), "bytes"

fp = open("samples/sample.jpg")
print len(fp.read()), "bytes"

结果如下:

3565 bytes
Traceback (innermost last):
  File "builtin-open-example-1.py", line 12, in ?
  File "builtin-open-example-1.py", line 5, in open
IOError: not a GIF file

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程

posted on 2018-03-28 10:11  go2coding  阅读(179)  评论(0编辑  收藏  举报

导航