Number of lines of code written in python

Source: Internet
Author: User
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! = "":
EachLine = 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;
LineCount = lineCount + 1;
Self. all + = lineCount;
Self. allComment + = commentCount;
Self. allBlank + = blkcount;
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 (Block comment is not supported in our language ).
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.

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.