统计python代码量

        今天闲着没事,花了一点时间写了一个程序来统计一个目录或多个目录下python文件的代码总行数、注释总行数、空行总行数、真正代码总行数。具体代码如下:

python 代码
  1. class CalculateCode():   
  2.     def __init__(self):   
  3.         self.total_count = 0   
  4.         self.code_count = 0   
  5.         self.comment_count = 0   
  6.         self.blank_count = 0   
  7.            
  8.     def calculate_file(self, filename):   
  9.         file = open(filename, 'r')   
  10.         lines = file.readlines()   
  11.         line_count = 0   
  12.         line_len = len(lines)   
  13.         self.total_count += line_len   
  14.         while line_count < line_len:   
  15.             line_without_space = lines[line_count].strip()   
  16.             if not line_without_space:   
  17.                 self.blank_count += 1   
  18.                 line_count += 1   
  19.             elif line_without_space.startswith('#'):   
  20.                 self.comment_count += 1   
  21.                 line_count += 1   
  22.             elif line_without_space.startswith("'''"):  
  23.                 self.comment_count += 1  
  24.                 line_count += 1  
  25.                 if not line_without_space.endswith("'''"):   
  26.                     while line_count < line_len:   
  27.                         comment_line = lines[line_count].strip()   
  28.                         self.comment_count += 1   
  29.                         line_count += 1   
  30.                         if comment_line.endswith("'''"):   
  31.                             break  
  32.             else:   
  33.                 self.code_count += 1   
  34.                 line_count += 1   
  35.                    
  36.     def calculate_directory(self, directory):   
  37.         import os  
  38.         if os.path.isfile(directory):   
  39.             if os.path.splitext(directory)[1] == '.py':   
  40.                 self.calculate_file(directory)   
  41.             else:   
  42.                 raise Exception('The file must be a python file')   
  43.         elif os.path.isdir(directory):   
  44.             for path in os.listdir(directory):   
  45.                 if os.path.isdir(directory+'\\'+path):   
  46.                     self.calculate_directory(directory+'\\'+path)   
  47.                 elif os.path.splitext(path)[1] == '.py':   
  48.                     self.calculate_file(directory+'\\'+path)   
  49.                 else:   
  50.                     pass  
  51.         else:   
  52.             raise Exception('No this directory or file!')   
  53.            
  54.     def calculate_directories(self, directories):   
  55.         for directory in directories:   
  56.             self.calculate_directory(directory)   
  57.                
  58.     def show_result(self):   
  59.         print 'total_count: %d\ncode_count: %d\ncomment_count: %d\nblank_count: %d\n' \   
  60.         %(self.total_count,self.code_count,self.comment_count,self.blank_count)    
  61.   
  62. if __name__ == '__main__':   
  63.     import sys  
  64.     if len(sys.argv) > 1:   
  65.         path_list = sys.argv[1:]   
  66.         calculate_code = CalculateCode()   
  67.         calculate_code.calculate_directories(path_list)   
  68.         calculate_code.show_result()   
  69.     else:   
  70.         print 'Please use command like this:\n"python calculatecode.py directory1 directory2 ..."'  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值