日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

Python腳本文件LineCount.py在實際運行的過程中會有很多簡捷的技巧可供我們大家借鑒,我們可以將其使用在python腳本文件中,既Python腳本文件LineCount.py,如果你對Python腳本文件感興趣的話,你就可以點擊以下的文章。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡營銷推廣、網(wǎng)站重做改版、渭濱網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、成都h5網(wǎng)站建設商城網(wǎng)站制作、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為渭濱等各大城市提供網(wǎng)站開發(fā)制作服務。

因為最近在作的項目很特殊,所使用的語言是一個公司內(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)容如下:

 
 
 
  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 :這句來判斷當前行是不是單行注釋(我們的這門語言不支持塊注釋)以上就是對Python腳本文件LineCount.py的相關代碼的介紹。為了能在其他機器上運行,使用了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"],
  8. )  

不過生成exe后程序臃腫很多,有3M多。感覺使用python確實是件很愜意的事。 以上的文章就是對python寫的代碼行數(shù)統(tǒng)計程序的相關內(nèi)容的介紹。


分享標題:Python腳本文件LineCount.py的相關代碼介紹
網(wǎng)頁路徑:http://m.5511xx.com/article/dhjicep.html