Line of Code statistics tool

Source: Internet
Author: User
Tags in python
To make it easier to count the lines of your own code, you implemented the gadget in Python, which enables you to count multiple files in a single file and a directory, and output the number of lines of code, blank lines, and comment lines. For code in different languages, it is only necessary to modify its annotation to correctly count the number of lines of information.
Import OS, RE, sys

Class Codecount:
"""
This are used to calculate total number of source,
Comment and Blank lines.
"""
def __init__ (self, path, commentsign = ' # '):
Self.path = Path
Self.all = 0
self.allcomment = 0
Self.allblank = 0
Self.allsource = 0
Self.commentsign = Commentsign

def Calculate (self):
If not Os.path.isdir (Self.path):
If Os.path.exists (Self.path):
Self. Countlines (Self.path)
else:
Print '%s is not a valid path or file name! '% path
Return
For Root, dirs, the files in Os.walk (Self.path):
For file in Files:
File = Os.path.join (root, file)
Self. Countlines (file)

def countlines (self, filename):
f = file (filename)
lines = F.readlines ()
Rblank = Re.compile (R ' ^s*$ ')
Rcomment = Re.compile (' ^s*%sw* '% self.commentsign)
Nblank = 0
ncomment = 0
Nsource = 0
For line in lines:
If Rblank.match (line) or not:
Nblank + 1
Elif Rcomment.match (line):
Ncomment + 1
else:
Nsource + 1

print filename;
print ' total: ', Nblank + ncomment + nsource
print ' Comment: ', ncomment
print ' Blank: ', Nblank
print ' Source: ', Nsource
Self.all + = (Nblank + ncomment + nsource)
Self.allblank + + Nblank
Self.allcomment + + ncomment
Self.allsource + + Nsource


def getcommentcount (self):
Return self.allcomment

def getblankcount (self):
Return Self.allblank

def getsourcecount (self):
Return Self.allsource

def getallcount (self):
Return Self.allblank + self.allcomment + self.allsource

def main ():
If Len (sys.argv) = = 2:
Return
# path = raw_input (' input a path or filename: ')
Path = sys.argv[1]
Ccut = Codecount (path)
Ccut.calculate ()
Print path, ': '
print ' total: ', Ccut.getallcount ()
print ' Comment: ', Ccut.getcommentcount ()
print ' Blank: ', Ccut.getblankcount ()
print ' Source: ', Ccut.getsourcecount ()

if __name__ = = ' __main__ ':
Main ()

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.