編寫Python程序?qū)崿F(xiàn)行數(shù)統(tǒng)計
當(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)容如下:
- import sys;
- import os;
- class LineCount:
- def trim(self,docstring):
- if not docstring:
- return ''
- lines = docstring.expandtabs().splitlines()
- indent = sys.maxint
- for line in lines[1:]:
- stripped = line.lstrip()
- if stripped:
- indent = min(indent, len(line) - len(stripped))
- trimmed = [lines[0].strip()]
- if indent < sys.maxint:
- for line in lines[1:]:
- trimmed.append(line[indent:].rstrip())
- while trimmed and not trimmed[-1]:
- trimmed.pop()
- while trimmed and not trimmed[0]:
- trimmed.pop(0)
- return '\n'.join(trimmed)
- def FileLineCount(self,filename):
- (filepath,tempfilename) = os.path.split(filename);
- (shotname,extension) = os.path.splitext(tempfilename);
- if extension == '.txt' or extension == '.hol' : # file type
- file = open(filename,'r');
- self.sourceFileCount += 1;
- allLines = file.readlines();
- file.close();
- lineCount =0;
- commentCount = 0;
- blankCount = 0;
- codeCount = 0;
- for eachLine in allLines:
- if eachLine != " " :
- eachLineeachLine = eachLine.replace(" ",""); #remove space
- eachLine = self.trim(eachLine); #remove tabIndent
- if eachLine.find('--') == 0 : #LINECOMMENT
- commentCount += 1;
- else :
- if eachLine == "":
- blankCount += 1;
- else :
- codeCount += 1;
- lineCountlineCount = lineCount + 1;
- self.all += lineCount;
- self.allComment += commentCount;
- self.allBlank += blankCount;
- self.allSource += codeCount;
- print filename;
- print ' Total :',lineCount ;
- print ' Comment :',commentCount;
- print ' Blank :',blankCount;
- print ' Source :',codeCount;
- def CalulateCodeCount(self,filename):
- if os.path.isdir(filename) :
- if not filename.endswith('\\'):
- filename += '\\';
- for file in os.listdir(filename):
- if os.path.isdir(filename + file):
- self.CalulateCodeCount(filename + file);
- else:
- self.FileLineCount(filename + file);
- else:
- self.FileLineCount(filename);
- # Open File
- def __init__(self):
- self.all = 0;
- self.allComment =0;
- self.allBlank = 0;
- self.allSource = 0;
- self.sourceFileCount = 0;
- filename = raw_input('Enter file name: ');
- self.CalulateCodeCount(filename);
- if self.sourceFileCount == 0 :
- print 'No Code File';
- pass;
- print '\n';
- print '***************** All Files **********************';
- print ' Files :',self.sourceFileCount;
- print ' Total :',self.all;
- print ' Comment :',self.allComment;
- print ' Blank :',self.allBlank;
- print ' Source :',self.allSource;
- print '****************************************************';
- 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)容如下:
- from distutils.core import setup
- import py2exe
- setup(
- version = "0.0.1",
- description = "LineCount",
- name = "LineCount",
- console = ["LineCount.py"],
- )
不過生成exe后程序臃腫很多,有3M多。感覺使用Python程序確實(shí)是件很愜意的事。