新聞中心
Python腳本文件LineCount.py在實(shí)際運(yùn)行的過(guò)程中會(huì)有很多簡(jiǎn)捷的技巧可供我們大家借鑒,我們可以將其使用在python腳本文件中,既Python腳本文件LineCount.py,如果你對(duì)Python腳本文件感興趣的話,你就可以點(diǎn)擊以下的文章。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、渭濱網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為渭濱等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
因?yàn)樽罱谧鞯捻?xiàng)目很特殊,所使用的語(yǔ)言是一個(gè)公司內(nèi)部的IDE環(huán)境,而這個(gè)IDE所產(chǎn)生的代碼并不是以文本方式存放的,都是放在二進(jìn)制文件中,而且由于這門語(yǔ)言外界幾乎接觸不到,所以沒有針對(duì)它的代碼統(tǒng)計(jì)程序,當(dāng)一個(gè)模塊完成后要統(tǒng)計(jì)代碼行數(shù)會(huì)很困難,要統(tǒng)計(jì)的話必須先把代碼編輯器中的內(nèi)容拷貝到一個(gè)文本類型的文件中。
正好一直在關(guān)注python,還沒有用python寫過(guò)程序,今天就利用中午休息的時(shí)間寫了一個(gè)簡(jiǎn)單的代碼統(tǒng)計(jì)程序。對(duì)輸入的路徑作遞歸,查找代碼文件,對(duì)每一個(gè)代碼文件計(jì)算它的注釋行數(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'這句是判斷文件的后綴,來(lái)確定是否要計(jì)算代碼行數(shù)。if eachLine.find('--') == 0 :這句來(lái)判斷當(dāng)前行是不是單行注釋(我們的這門語(yǔ)言不支持塊注釋)以上就是對(duì)Python腳本文件LineCount.py的相關(guān)代碼的介紹。為了能在其他機(jī)器上運(yùn)行,使用了py2exe來(lái)把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"],
- )
不過(guò)生成exe后程序臃腫很多,有3M多。感覺使用python確實(shí)是件很愜意的事。 以上的文章就是對(duì)python寫的代碼行數(shù)統(tǒng)計(jì)程序的相關(guān)內(nèi)容的介紹。
新聞名稱:Python腳本文件LineCount.py的相關(guān)代碼介紹
分享URL:http://www.dlmjj.cn/article/dhjicep.html


咨詢
建站咨詢
