代码行数统计工具

为了便于统计自己代码的行数,用python实现了该小工具,可以支持对单个文件和一个目录下的多个文件进行统计,并输出统计的代码行数,空行数和注释行数。对不同语言的代码,只需要修改其注释符就可以正确的统计行数信息。
import  os, re, sys

class  CodeCount:
    
"""
    this py is used to calculate the 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, 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  line:
                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()
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值