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

編寫Python程序?qū)崿F(xiàn)行數(shù)統(tǒng)計

開發(fā) 后端
我們在這里編寫的Python程序主要是實(shí)現(xiàn)對代碼行數(shù)的統(tǒng)計。希望初學(xué)者們可以通過本文介紹的內(nèi)容,加深對這一語言的認(rèn)知程度。

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

正好一直在關(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ī)器上運(yùn)行,使用了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程序確實(shí)是件很愜意的事。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2019-08-01 15:08:37

PythonLine操作系統(tǒng)

2015-07-22 12:42:36

Pivot行列轉(zhuǎn)換

2011-06-16 10:09:25

QT Windows DLL

2020-12-14 13:24:17

PandasSQL數(shù)據(jù)集

2011-06-27 13:57:42

JavaScript

2022-06-24 09:58:35

大數(shù)據(jù)JavaPython

2010-02-03 09:27:21

編寫Python程序

2009-12-08 18:01:00

曙光移動集中采購

2012-09-13 10:44:18

Python代碼

2022-11-17 10:23:13

VS CodeCodiumPython

2017-11-20 14:46:27

命令代碼

2010-03-04 09:49:58

Python Hell

2010-03-09 10:49:35

python簡單應(yīng)用

2015-05-08 13:09:12

JavaScriipt抽獎程序

2024-08-06 09:40:21

2011-06-16 17:54:30

Qt Mplayer

2017-05-08 10:38:36

PythonJavaScriptWIFI

2010-03-04 15:45:56

Python程序調(diào)試

2011-11-08 10:13:20

2011-03-02 15:35:15

Oracle分組統(tǒng)計
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號