def zip_import(): import zipfile, tempfile, os, sys handle, fname = tempfile.mkstemp(".zip") os.close(handle) z = zipfile.ZipFile(fname, "w") z.writestr("hello.py", "def f(): return 'hello world from ' + __file__\n") z.close() sys.path.insert(0, fname) # zip路径加入 import hello # 导入模块 print hello.f() os.unlink(fname)
def testf(): t = time.time() for i in xrange(1000000): fpy(i) print time.time()-t t= time.time() for i in xrange(1000000): f(i) print time.time()-t t= time.time() for i in xrange(1000000): f2(i) print time.time()-t
def testf(): t = time.time() for i in xrange(1000000): fpy(i) print time.time() - t t = time.time() for i in xrange(1000000): f(i) print time.time() - t t = time.time() for i in xrange(1000000): f2(i) print time.time() - t
for filename in z.namelist(): print 'File:',filename, bytes = z.read(filename) print 'has', len(bytes), 'bytes' #zipfile模块现在还不能处理分卷zip文件和带有注释的zip文件,注意,要使用r作为标志参数,而不是rb #如果zip文件中包含一些Python模块,(.py或.pyc)也许还有一些其他数据文件,可以把这个文件的路径加入到Python的sys.path中 #并用import语句来导入处于这个zip文件中的模块。示例: import zipfile, tempfile, os, sys handle, filename = tempfile.mkstemp('.zip') os.close(handle) z = zipfile.ZipFile(filename, 'w') z.writestr('hello.py', 'def f():return "hello world from "+__file__\n') z.close() sys.path.insert(0, filename) import hello print hello.f() os.unlink(filename) ''' 2.10 处理字符串中的zip文件 ''' #解决这种问题,正是Python标准库的cStringIO模块的拿手好戏 import cStringIO, zipfile class ZipString(ZipFile): def __init__(self, datastring): ZipFile.__init__(self, cStringIO.StringIO(datastring)) #cStringIO模块可以将一串字节封装起来,让你像访问文件对象一样访问其中的数据 import zipfile try: from cStringIO import StringIO except ImportError: from StringIO import StringIO
def test_f(self): result = f(True, False) self.assertEqual(result, 1)
import hello from pessoas import Pessoa print(type(hello)) hello.f() print(type(Pessoa()))
''' from hello import say_hello_to, f, f2 import time def fpy(x): return x**2-x def testf(): t = time.time() for i in xrange(1000000): fpy(i) print time.time()-t t= time.time() for i in xrange(1000000): f(i) print time.time()-t t= time.time() for i in xrange(1000000): f2(i) print time.time()-t if __name__ == '__main__': say_hello_to("Chenhh") print f(10) testf()
def import_and_run_module(): import hello print hello.f()
def f(x): return x**2-x def integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx s = time.time() integrate_f(10**7, 10**7, 10**7) print(time.time() - s) s = time.time() cy.integrate_f(10**7, 10**7, 10**7) print(time.time() - s) s = time.time() cy.integrate_f2(10**7, 10**7, 10**7) print(time.time() - s) s = time.time() cy.integrate_f3(10**7, 10**7, 10**7) print(time.time() - s) cy.f(2) cy.f2(2) #cy.f3(2) #cdefで定義しているのでpython側からは見えない。
''' from hello import say_hello_to, f, f2 import time def fpy(x): return x**2 - x def testf(): t = time.time() for i in xrange(1000000): fpy(i) print time.time() - t t = time.time() for i in xrange(1000000): f(i) print time.time() - t t = time.time() for i in xrange(1000000): f2(i) print time.time() - t if __name__ == '__main__': say_hello_to("Chenhh") print f(10) testf()