日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python腳本文件LineCount.py的相關(guān)代碼介紹

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)容如下:

 
 
 
  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'這句是判斷文件的后綴,來(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)容如下:

 
 
 
  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"],
  8. )  

不過(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