自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python腳本文件LineCount.py的相關代碼介紹

開發(fā) 后端
以下的文章是通過Python腳本文件LineCount.py的相關實際代碼來介紹Python腳本文件LineCount.py在實際操作中的具體應用的介紹。

Python腳本文件LineCount.py在實際運行的過程中會有很多簡捷的技巧可供我們大家借鑒,我們可以將其使用在python腳本文件中,既Python腳本文件LineCount.py,如果你對Python腳本文件感興趣的話,你就可以點擊以下的文章。

因為最近在作的項目很特殊,所使用的語言是一個公司內(nèi)部的IDE環(huán)境,而這個IDE所產(chǎn)生的代碼并不是以文本方式存放的,都是放在二進制文件中,而且由于這門語言外界幾乎接觸不到,所以沒有針對它的代碼統(tǒng)計程序,當一個模塊完成后要統(tǒng)計代碼行數(shù)會很困難,要統(tǒng)計的話必須先把代碼編輯器中的內(nèi)容拷貝到一個文本類型的文件中。

正好一直在關注python,還沒有用python寫過程序,今天就利用中午休息的時間寫了一個簡單的代碼統(tǒng)計程序。對輸入的路徑作遞歸,查找代碼文件,對每一個代碼文件計算它的注釋行數(shù),空行數(shù),真正的代碼行數(shù)。自己用的程序,就寫的粗糙了,也沒加異常處理。

主要的Python腳本文件LineCount.py的內(nèi)容如下:

  1. import sys;  
  2. import os;  
  3.  
  4. class LineCount:  
  5. def trim(self,docstring):  
  6. if not docstring:  
  7. return ''  
  8. lines = docstring.expandtabs().splitlines()  
  9.  
  10. indent = sys.maxint  
  11. for line in lines[1:]:  
  12. stripped = line.lstrip()  
  13. if stripped:  
  14. indent = min(indent, len(line) - len(stripped))  
  15.  
  16. trimmed = [lines[0].strip()]  
  17. if indent < sys.maxint: 
  18. for line in lines[1:]:  
  19. trimmed.append(line[indent:].rstrip())  
  20.  
  21. while trimmed and not trimmed[-1]:  
  22. trimmed.pop()  
  23. while trimmed and not trimmed[0]:  
  24. trimmed.pop(0)  
  25.  
  26. return '\n'.join(trimmed)  
  27.  
  28. def FileLineCount(self,filename):  
  29. (filepath,tempfilename) = os.path.split(filename);  
  30. (shotname,extension) = os.path.splitext(tempfilename);  
  31. if extension == '.txt' or extension == '.hol' : # file type   
  32. file = open(filename,'r');  
  33. self.sourceFileCount += 1;  
  34. allLines = file.readlines();  
  35. file.close();  
  36.  
  37. lineCount =0;  
  38. commentCount = 0;  
  39. blankCount = 0;  
  40. codeCount = 0;  
  41. for eachLine in allLines:  
  42. if eachLine != " " :  
  43. eachLineeachLine = eachLine.replace(" ",""); #remove space  
  44. eachLine = self.trim(eachLine); #remove tabIndent  
  45. if eachLine.find('--') == 0 : #LINECOMMENT   
  46. commentCount += 1;  
  47. else :  
  48. if eachLine == "":  
  49. blankCount += 1;  
  50. else :  
  51. codeCount += 1;  
  52. lineCountlineCount = lineCount + 1;  
  53. self.all += lineCount;  
  54. self.allComment += commentCount;  
  55. self.allBlank += blankCount;  
  56. self.allSource += codeCount;  
  57. print filename;  
  58. print ' Total :',lineCount ;  
  59. print ' Comment :',commentCount;  
  60. print ' Blank :',blankCount;  
  61. print ' Source :',codeCount;  
  62.  
  63. def CalulateCodeCount(self,filename):  
  64. if os.path.isdir(filename) :  
  65. if not filename.endswith('\\'):  
  66. filename += '\\';   
  67. for file in os.listdir(filename):  
  68. if os.path.isdir(filename + file):  
  69. self.CalulateCodeCount(filename + file);  
  70. else:  
  71. self.FileLineCount(filename + file);  
  72. else:  
  73. self.FileLineCount(filename);  
  74.  
  75. # Open File  
  76. def __init__(self):  
  77. self.all = 0;  
  78. self.allComment =0;  
  79. self.allBlank = 0;  
  80. self.allSource = 0;  
  81. self.sourceFileCount = 0;  
  82. filename = raw_input('Enter file name: ');  
  83. self.CalulateCodeCount(filename);  
  84. if self.sourceFileCount == 0 :  
  85. print 'No Code File';  
  86. pass;  
  87. print '\n';  
  88. print '***************** All Files **********************';  
  89. print ' Files :',self.sourceFileCount;  
  90. print ' Total :',self.all;  
  91. print ' Comment :',self.allComment;  
  92. print ' Blank :',self.allBlank;  
  93. print ' Source :',self.allSource;  
  94. print '****************************************************';  
  95.  
  96. myLineCount = LineCount(); 

可以看到extension == '.txt' or extension == '.hol'這句是判斷文件的后綴,來確定是否要計算代碼行數(shù)。if eachLine.find('--') == 0 :這句來判斷當前行是不是單行注釋(我們的這門語言不支持塊注釋)以上就是對Python腳本文件LineCount.py的相關代碼的介紹。為了能在其他機器上運行,使用了py2exe來把python腳本生成可執(zhí)行的exe,setup.py腳本內(nèi)容如下:

  1. from distutils.core import setup  
  2. import py2exe  
  3. setup(  
  4. version = "0.0.1",  
  5. description = "LineCount",  
  6. name = "LineCount",  
  7. console = ["LineCount.py"],  
  8. )   

 

不過生成exe后程序臃腫很多,有3M多。感覺使用python確實是件很愜意的事。 以上的文章就是對python寫的代碼行數(shù)統(tǒng)計程序的相關內(nèi)容的介紹。

【編輯推薦】

  1. 對Python源代碼組織的相關實際操作步驟解析
  2. Python安裝的步驟操作其實是件很容易的事
  3. 對Python源代碼組織的相關實際操作步驟解析
  4. Python編程語言在Zope軟件開發(fā)中具體應用
  5. Python嵌入C/C++在釋放資源中的實際操作步驟詳解
責任編輯:佚名 來源: 互聯(lián)網(wǎng)
相關推薦

2010-04-08 09:27:44

Oracle 安裝腳本

2010-02-01 16:32:49

Python腳本

2010-03-19 14:44:30

Python模塊級函數(shù)

2010-03-25 13:19:57

Python_ast.

2009-12-03 10:06:33

Ubuntushell腳本

2009-06-12 18:30:12

Groovy 靜態(tài)ma

2010-03-26 15:28:05

Python編寫

2010-03-29 17:37:17

Nginx resin

2010-01-15 16:21:45

VB.NET讀寫文本文

2010-03-25 10:13:03

Python代碼

2010-03-23 13:50:24

python教程

2010-03-25 12:50:45

Python代碼

2010-03-26 16:17:24

Python嵌入

2010-03-23 08:56:38

Python隨機數(shù)模塊

2010-03-11 16:50:27

Python應用

2010-03-26 16:35:29

Python open

2010-03-19 16:51:53

Java Socket

2010-03-17 19:24:38

Java多線程循環(huán)

2009-02-01 10:29:04

Oracle數(shù)據(jù)庫管理

2021-04-24 23:00:43

Windows 10Windows微軟
點贊
收藏

51CTO技術棧公眾號