Python静态编译器说明研究

开发 后端
本文对Python的版本选择,IDE选择及编码的解决方案进行了一番详细的描述,实为Python初学者必读的Python学习经验心得。

使用了wxPython、pychecker编写的Python静态编译器,用于在编译器查找py脚本的错误,开放源码,与各位pythoner共享之,希望本文能给大家带来帮助,下面就一起进去Python的世界吧。

代码如下:

  1.     def OnBuildOne(self, event):  
  2.         if self.paths.count != 0:  
  3.             self.Report.AppendText(self.CompileInfoHead("File"))  
  4.             path = self.paths[self.List.GetSelection()]  
  5.             print "Building " + path + " ..."  
  6.             try:  
  7.                 py_compile.compile(path, None, None)  
  8.             except py_compile.PyCompileError, ex:  
  9.                 print ex  
  10.             self.Report.AppendText("=-- Build Finished.\n\n")  
  11.  
  12.  
  13.     def OnBuildAll(self, event):  
  14.         if self.paths.count != 0:  
  15.             self.Report.AppendText(self.CompileInfoHead("File(s)"))  
  16.             for path in self.paths:  
  17.                 print "Building " + path + " ..."  
  18.                 try:  
  19.                     py_compile.compile(path, None, None)  
  20.                 except py_compile.PyCompileError, ex:  
  21.                     print ex  
  22.             self.Report.AppendText("=-- Build Finished.\n\n")  
  23.  
  24.  
  25.     def OnBuildDirectory(self, event):  
  26.         dlg = wxDirDialog(self, "Select a directory for build", self.cfg[2])  
  27.         if dlg.ShowModal() == wxID_OK:  
  28.             path = dlg.GetPath()  
  29.             self.Report.AppendText(self.CompileInfoHead("Directory:", path))  
  30.             compile_dir(path, 10, None, 1, None)  
  31.             self.Report.AppendText("=-- Build Finished.\n\n")  
  32.             self.cfg[2] = dlg.GetPath()  
  33.                   
  34.         dlg.Destroy()  
  35.       
  36.  
  37.     def OnAbout(self, event):   
  38.         dlg = wxMessageDialog(self, "Present by Dracula 2005\n"   
  39.                                     "Build 2005.05.05\n", "About",   
  40.                                     wxOK | wxICON_INFORMATION)  
  41.         dlg.ShowModal()  
  42.         dlg.Destroy()  
  43.  
  44.  
  45.     def OnResize(self, event):  
  46.         sizeClient = self.GetClientSize()  
  47.         self.List.SetSize(sizeClient)  
  48.         sizeList = self.List.GetClientSize()  
  49.         self.Report.SetSize(wxSize(sizeClient.width, sizeClient.height-sizeList.height))  
  50.  
  51.  
  52.     def OnClose(self, event):  
  53.         try:  
  54.             f = open("config.cfg", "w")  
  55.             f.write(self.cfg[0])  
  56.             if self.cfg[0][-1] != '\n':  
  57.                 f.write("\n")  
  58.             f.write(self.cfg[1])  
  59.             if self.cfg[1][-1] != '\n':  
  60.                 f.write("\n")  
  61.             f.write(self.cfg[2])  
  62.             f.close()  
  63.         except IOError:  
  64.             pass  
  65.  
  66.         sys.path = self.save_sys_path[:]  
  67.           
  68.         self.timer.Stop()  
  69.         del self.timer   
  70.         del self.icon   
  71.         self.Destroy()  
  72.  
  73.  
  74.     def OnQuit(self, event):  
  75.         self.Close(true)  
  76.  
  77.  
  78.     def PyCheck(self, argv):  
  79.         argv2 = ['pychecker']  
  80.         argv2.append(argv)  
  81.         pychecker.checker2.main(argv2)  
  82.         #reload(pychecker.checker2)  
  83.  
  84.  
  85.     def AddPath(self, path):  
  86.         curdir = path 
  87.         system_dir = curdir + '\\data\\script'  
  88.         system_core_dir = curdir + '\\data\\script\\core'  
  89.         subsystem_dir = curdir + '\\data\\subsystem'  
  90.         subsystem_trashbin_dir = curdir + '\\data\\subsystem\\trashbin'  
  91.  
  92.         sys.path = self.save_sys_path[:]  
  93.         sys.path.append(curdir)  
  94.         sys.path.append(system_dir)  
  95.         sys.path.append(system_core_dir)  
  96.         sys.path.append(subsystem_dir)  
  97.         sys.path.append(subsystem_trashbin_dir)  
  98.  
  99.  
  100.     def CompileInfoHead(self, str1, str2=""):  
  101.         return "=-- %s %s Compile %s %s ...\n" % (self.Date(), self.Time(), str1, str2)  
  102.       
  103.  
  104.     def Error(self, error):  
  105.         self.Report.AppendText(error)  
  106.  
  107.  
  108.     def Output(self, info):  
  109.         self.Report.AppendText(info)  
  110.  
  111.  
  112.     def Date(self):  
  113.         t = time.localtime(time.time())   
  114.         strDate = time.strftime("%Y.%m.%d", t)  
  115.         return strDate  
  116.  
  117.  
  118.     def Time(self):  
  119.         t = time.localtime(time.time())   
  120.         strTime = time.strftime("%I:%M:%S", t)  
  121.         return strTime  
  122.  
  123.  
  124.     def Notify(self):  
  125.         self.statusbar.SetStatusText(self.Date() + "   " + self.Time(), 1)  
  126.  
  127.  
  128. class MyApp(wxApp):  
  129. def OnInit(self):  
  130. self.frame = MyFrame(NULL, -1, "cd2Py Compiler")  
  131. self.frame.Show(true)  
  132. return true   
  133. cd2Py = MyApp(0)  
  134. import sys  
  135. class errCatcher:  
  136. def __init__(self):  
  137. pass  
  138. def write(self, stuff):  
  139. cd2Py.frame.Error(stuff)  
  140. class outCatcher:  
  141. def __init__(self):  
  142. passdef write(self, stuff):  
  143. cd2Py.frame.Output(stuff)  
  144. sys.stderr = errCatcher()  
  145. sys.stdout = outCatcher()  
  146. cd2Py.MainLoop() 

【编辑推荐】

  1. 如何使Python嵌入C++应用程序?
  2. 深入探讨Ruby与Python语法比较
  3. Python学习资料介绍分享
  4. Python学习经验谈:版本、IDE选择及编码解决方案
  5. 浅析Python的GIL和线程安全
责任编辑:chenqingxiang 来源: 人民邮电出版社
相关推荐

2010-02-02 17:08:26

Python静态编译器

2010-01-18 10:34:21

C++编译器

2023-07-31 07:33:04

Rust编译器内存

2010-02-03 15:30:03

IronPython

2010-01-28 15:56:38

VC++ 6.0编译

2010-01-13 17:12:26

C++编译器

2010-03-23 11:17:16

Python 动态编译

2022-12-28 08:52:15

编译器自动内存管理

2010-01-13 14:35:10

Visual C++

2021-03-15 14:54:47

编译器工具代码

2021-10-17 19:52:40

Python:源码编译器

2010-01-21 09:11:38

C++编译器

2009-08-10 17:12:54

C#编译器

2013-03-29 10:02:37

编译器语言编译开发

2017-03-20 18:01:55

编译器汇编

2020-01-10 18:04:01

Python编程语言Windows

2009-12-11 15:38:40

VS2008编译器

2010-10-20 13:43:37

C++编译器

2019-08-06 08:20:07

编译器工具开发者

2010-02-23 15:44:24

Python编辑器
点赞
收藏

51CTO技术栈公众号