Code introduction to the LineCount. py Python script file

Source: Internet
Author: User

LineCount. in the actual running process, py has a lot of simple skills for your reference. We can use it in the python script file, that is, the Python script file LineCount. py, if you are interested in Python script files, you can click the following article.

Because the projects recently made are very special, the language used is a company's internal IDE environment, and the code produced by this IDE is not stored in text, they are all placed in binary files, and the language is almost invisible to the outside, so there is no code statistics program for it. It is very difficult to count the number of lines of code after a module is completed, to collect statistics, you must first copy the content in the code editor to a text file.

I have been paying attention to python and haven't written a program in python. Today I wrote a simple code statistics program using the noon break. Recursively search the input path, find the code file, and calculate the number of lines of comments, empty lines, and real lines of code for each code file. If you use a program, you can write it roughly without exception handling.

The main Python script file LineCount. py contains the following content:

 
 
  1. import sys;  
  2. import os;  
  3.  
  4. class LineCount:  
  5. def trim(self,docstring):  
  6. if not docstring:  
  7. return ''  
  8. lines = docstring.expandtabs().splitlines()  
  9.  
  10. indent = sys.maxint  
  11. for line in lines[1:]:  
  12. stripped = line.lstrip()  
  13. if stripped:  
  14. indent = min(indent, len(line) - len(stripped))  
  15.  
  16. trimmed = [lines[0].strip()]  
  17. if indent < sys.maxint: 
  18. for line in lines[1:]:  
  19. trimmed.append(line[indent:].rstrip())  
  20.  
  21. while trimmed and not trimmed[-1]:  
  22. trimmed.pop()  
  23. while trimmed and not trimmed[0]:  
  24. trimmed.pop(0)  
  25.  
  26. return '\n'.join(trimmed)  
  27.  
  28. def FileLineCount(self,filename):  
  29. (filepath,tempfilename) = os.path.split(filename);  
  30. (shotname,extension) = os.path.splitext(tempfilename);  
  31. if extension == '.txt' or extension == '.hol' : # file type   
  32. file = open(filename,'r');  
  33. self.sourceFileCount += 1;  
  34. allLines = file.readlines();  
  35. file.close();  
  36.  
  37. lineCount =0;  
  38. commentCount = 0;  
  39. blankCount = 0;  
  40. codeCount = 0;  
  41. for eachLine in allLines:  
  42. if eachLine != " " :  
  43. eachLineeachLine = eachLine.replace(" ",""); #remove space  
  44. eachLine = self.trim(eachLine); #remove tabIndent  
  45. if eachLine.find('--') == 0 : #LINECOMMENT   
  46. commentCount += 1;  
  47. else :  
  48. if eachLine == "":  
  49. blankCount += 1;  
  50. else :  
  51. codeCount += 1;  
  52. lineCountlineCount = lineCount + 1;  
  53. self.all += lineCount;  
  54. self.allComment += commentCount;  
  55. self.allBlank += blankCount;  
  56. self.allSource += codeCount;  
  57. print filename;  
  58. print ' Total :',lineCount ;  
  59. print ' Comment :',commentCount;  
  60. print ' Blank :',blankCount;  
  61. print ' Source :',codeCount;  
  62.  
  63. def CalulateCodeCount(self,filename):  
  64. if os.path.isdir(filename) :  
  65. if not filename.endswith('\\'):  
  66. filename += '\\';   
  67. for file in os.listdir(filename):  
  68. if os.path.isdir(filename + file):  
  69. self.CalulateCodeCount(filename + file);  
  70. else:  
  71. self.FileLineCount(filename + file);  
  72. else:  
  73. self.FileLineCount(filename);  
  74.  
  75. # Open File  
  76. def __init__(self):  
  77. self.all = 0;  
  78. self.allComment =0;  
  79. self.allBlank = 0;  
  80. self.allSource = 0;  
  81. self.sourceFileCount = 0;  
  82. filename = raw_input('Enter file name: ');  
  83. self.CalulateCodeCount(filename);  
  84. if self.sourceFileCount == 0 :  
  85. print 'No Code File';  
  86. pass;  
  87. print '\n';  
  88. print '***************** All Files **********************';  
  89. print ' Files :',self.sourceFileCount;  
  90. print ' Total :',self.all;  
  91. print ' Comment :',self.allComment;  
  92. print ' Blank :',self.allBlank;  
  93. print ' Source :',self.allSource;  
  94. print '****************************************************';  
  95.  
  96. myLineCount = LineCount(); 

We can see that extension = '.txt 'or extension ='. hol 'is used to determine the file suffix and determine whether to calculate the number of lines of code. If eachLine. find ('--') = 0: This statement is used to determine whether the current row is a single line comment. Our language does not support block comment.) The above is the LineCount Of The Python script file. py. To run on other machines, py2exe is used to generate an executable exe from the python script. The content of the setup. py script is as follows:

 
 
  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. )   

However, after the exe is generated, the program is too bloated, with more than 3 m. I feel that using python is indeed a pleasant task. The above article introduces the related content of the code line statistics program written in python.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.