`
dongin
  • 浏览: 120272 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

统计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 ..."'  
分享到:
评论

相关推荐

    代码量统计程序,支持C、Python、PHP、Java

    统计你的代码量,支持C、Python、PHP、Java统计你的代码量,支持C、Python、PHP、Java统计你的代码量,支持C、Python、PHP、Java

    C/C++/JAVA/Python代码统计工具

    该工具支持常用C/C++/JAVA/PYTHON/PERL等流行语言的代码统计工具,便于项目统计和项目度量输出。内附工具使用教程和命令参数,可以支持代码差异统计

    北京地铁客流量统计(py爬虫+js统计图)-爬虫python代码

    使用python3爬数据,echart统计图 爬虫的结果直接存到了文件中,因为项目比较小,就不用数据库了 爬虫脚本只是爬昨天的数据的,需要所有的要改一下get_flow_from_html()函数 def get_flow_from_html(html): # ...

    python项目--代码统计器

    python项目,代码统计器。统计代码量的一个程序。里面有全部详细步骤。

    Prediction-Strength-and-Gap-Statistics-in-Python:聚类中k估计的Gap统计和预测强度的Python实现

    Python中的预测强度和差距统计Gap统计和预测强度的Python实现,用于估计聚类中的k。

    python实现统计代码行数的方法

    主要介绍了python实现统计代码行数的方法,涉及Python中os模块及codecs模块的相关使用技巧,需要的朋友可以参考下

    python3 用户星标项目的代码量统计

    统计星标项目的用户代码量

    python3使用GUI统计代码量

    本文实例为大家分享了python3使用GUI统计代码量的具体代码,供大家参考,具体内容如下 # coding=utf-8 ''' 选择一个路径 遍历路径下的每一个文件,统计代码量 字典存储 每一种类型文件的代码行数,eg: *.py -&gt; ...

    使用python3通过python-gitlab的API来获取gitlab的仓库、用户等信息

    在我之前python27的基本上修改了下支持python3.6,使用python-gitlab的API来获取gitlab上用户、用户组、project的信息,压缩包了有现成的py脚本,可以直接下载

    代码量统计-包含python java vue

    ['.py', '.java', '.scss', '.css', '.js', '.vue', '.ts', '.json', '.xml', 'yaml', '.pom']

    代码统计工具(SourceCounter-3.5.33.73)

    免费的源代码统计工具,能统计包括:行数、工数、成本、质量指标等信息,支持20多种代码格式。 具有统计迅速、准确的优点,是程序开发人员的必备工具。可以对 C++、VB.Net、VB、C#、JAVA、Delphi、ASP.Net、ASP、...

    iris.csv数据集和python代码

    附件包含了原始的csv文件,和用于测试的python代码,适合统计学习入门。 鸢尾花(Iris)数据集是一个著名的统计学资料,被机器学习研究人员大量使用。它包含了150组实例,4种生物特征和每组实例对应的鸢尾花种类...

    python统计指定目录内文件的代码行数

    这符合一些公司的小需求,实际代码量的统计工作 效果如图 代码如下: #coding:utf-8 import os,re #代码所在目录 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件,统计其中的代码...

    python数据分析随书代码

    8.1 基于sqlite3的轻量级访问 181 8.2 通过pandas访问数据库 183 8.3 SQLAlchemy 185 8.3.1 SQLAlchemy的安装和配置 186 8.3.2 通过SQLAlchemy填充数据库 188 8.3.3 通过SQLAlchemy查询数据库 189 8.4 Pony ...

    毕业设计:基于python的百万级手机大数据分析与可视化

    2、统计 app 最活跃日期及当前使用量。 3、统计 app 最活跃时间段及每个时间段使用情况。 4、分析 app 最活跃地区及该地区范围使用量情况。 5、将使用量最高的 app 统计显示其类别。 6、建立性别年龄预测模型并检验...

    分享python数据统计的一些小技巧

    最近在用python做数据统计,这里总结了一些最近使用时查找和总结的一些小技巧,希望能帮助在做这方面时的一些童鞋。有些技巧是很平常的用法,平时我们没有注意,但是在特定场景,这些小方法还是能带来很大的帮助。 1...

    数据可视化教学代码和案例(python+jupyter)

    数据可视化的教学代码: 包括折线图+柱状图+饼图+盒图+散点图+直方图+3d图 画布的布局、画图中中文乱码的解决、背景板的选择等 简单的数据分析和可视化案例: titanic数据的简单分析可视化 iris数据的聚类可视化 GDP...

    数据平稳性ADF检验(基于Python编程语言实现)

    '''进行ADF检验 adf_test的返回值 Test statistic:代表检验统计量 ...(4)看检验统计量和临界值,检验统计量小于临界值的话,就拒绝原假设,认为序列是平稳的;大于的话,不能拒绝,认为是不平稳的

    Python语言程序设计基础(第2版)

    从绘制蟒蛇、理解天天向上的力量到机器学习、网络爬虫,从文本进度条、统计名著人物重要性到图像手绘效果、雷达图绘制,绝大多数实例为作者原创,将随着内容深入不断激发读者学习Python语言的热情,因为“编程是件很...

Global site tag (gtag.js) - Google Analytics