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)容如下:
- 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 :這句來判斷當前行是不是單行注釋(我們的這門語言不支持塊注釋)以上就是對Python腳本文件LineCount.py的相關代碼的介紹。為了能在其他機器上運行,使用了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確實是件很愜意的事。 以上的文章就是對python寫的代碼行數(shù)統(tǒng)計程序的相關內(nèi)容的介紹。
【編輯推薦】