日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
編寫Python程序?qū)崿F(xiàn)行數(shù)統(tǒng)計

當(dāng)我們在使用Python編程語言進(jìn)行程序開發(fā)的時候,會發(fā)現(xiàn)這一功能強(qiáng)大的語言可以給我們帶來非常大的作用。那么,接下來我們將會通過一個Python程序的實現(xiàn),來仔細(xì)分析一下這語言給我們帶來的獨特之處。#t#

為羅甸等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及羅甸網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、做網(wǎng)站、羅甸網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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

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

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

可以看到extension == '.txt' or extension == '.hol'這句是判斷文件的后綴,來確定是否要計算代碼行數(shù)。if eachLine.find('--') == 0 :這句來判斷當(dāng)前行是不是單行注釋(我們的這門語言不支持塊注釋)。為了能在其他機(jī)器上運行,使用了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"],  

不過生成exe后程序臃腫很多,有3M多。感覺使用Python程序確實是件很愜意的事。


本文標(biāo)題:編寫Python程序?qū)崿F(xiàn)行數(shù)統(tǒng)計
本文鏈接:http://www.5511xx.com/article/ccssdgo.html