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:
- 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();
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:
- from distutils.core import setup
- import py2exe
- setup(
- version = "0.0.1",
- description = "LineCount",
- name = "LineCount",
- console = ["LineCount.py"],
- )
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.