今天闲着没事,花了一点时间写了一个程序来统计一个目录或多个目录下python文件的代码总行数、注释总行数、空行总行数、真正代码总行数。具体代码如下:
python 代码
- class CalculateCode():
- def __init__(self):
- self.total_count = 0
- self.code_count = 0
- self.comment_count = 0
- self.blank_count = 0
-
- def calculate_file(self, filename):
- file = open(filename, 'r')
- lines = file.readlines()
- line_count = 0
- line_len = len(lines)
- self.total_count += line_len
- while line_count < line_len:
- line_without_space = lines[line_count].strip()
- if not line_without_space:
- self.blank_count += 1
- line_count += 1
- elif line_without_space.startswith('
- self.comment_count += 1
- line_count += 1
- elif line_without_space.startswith("'''"):
- self.comment_count += 1
- line_count += 1
- if not line_without_space.endswith("'''"):
- while line_count < line_len:
- comment_line = lines[line_count].strip()
- self.comment_count += 1
- line_count += 1
- if comment_line.endswith("'''"):
- break
- else:
- self.code_count += 1
- line_count += 1
-
- def calculate_directory(self, directory):
- import os
- if os.path.isfile(directory):
- if os.path.splitext(directory)[1] == '.py':
- self.calculate_file(directory)
- else:
- raise Exception('The file must be a python file')
- elif os.path.isdir(directory):
- for path in os.listdir(directory):
- if os.path.isdir(directory+'\\'+path):
- self.calculate_directory(directory+'\\'+path)
- elif os.path.splitext(path)[1] == '.py':
- self.calculate_file(directory+'\\'+path)
- else:
- pass
- else:
- raise Exception('No this directory or file!')
-
- def calculate_directories(self, directories):
- for directory in directories:
- self.calculate_directory(directory)
-
- def show_result(self):
- print 'total_count: %d\ncode_count: %d\ncomment_count: %d\nblank_count: %d\n' \
- %(self.total_count,self.code_count,self.comment_count,self.blank_count)
-
- if __name__ == '__main__':
- import sys
- if len(sys.argv) > 1:
- path_list = sys.argv[1:]
- calculate_code = CalculateCode()
- calculate_code.calculate_directories(path_list)
- calculate_code.show_result()
- else:
- 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/C++/JAVA/PYTHON/PERL等流行语言的代码统计工具,便于项目统计和项目度量输出。内附工具使用教程和命令参数,可以支持代码差异统计
使用python3爬数据,echart统计图 爬虫的结果直接存到了文件中,因为项目比较小,就不用数据库了 爬虫脚本只是爬昨天的数据的,需要所有的要改一下get_flow_from_html()函数 def get_flow_from_html(html): # ...
python项目,代码统计器。统计代码量的一个程序。里面有全部详细步骤。
Python中的预测强度和差距统计Gap统计和预测强度的Python实现,用于估计聚类中的k。
主要介绍了python实现统计代码行数的方法,涉及Python中os模块及codecs模块的相关使用技巧,需要的朋友可以参考下
统计星标项目的用户代码量
本文实例为大家分享了python3使用GUI统计代码量的具体代码,供大家参考,具体内容如下 # coding=utf-8 ''' 选择一个路径 遍历路径下的每一个文件,统计代码量 字典存储 每一种类型文件的代码行数,eg: *.py -> ...
在我之前python27的基本上修改了下支持python3.6,使用python-gitlab的API来获取gitlab上用户、用户组、project的信息,压缩包了有现成的py脚本,可以直接下载
['.py', '.java', '.scss', '.css', '.js', '.vue', '.ts', '.json', '.xml', 'yaml', '.pom']
免费的源代码统计工具,能统计包括:行数、工数、成本、质量指标等信息,支持20多种代码格式。 具有统计迅速、准确的优点,是程序开发人员的必备工具。可以对 C++、VB.Net、VB、C#、JAVA、Delphi、ASP.Net、ASP、...
附件包含了原始的csv文件,和用于测试的python代码,适合统计学习入门。 鸢尾花(Iris)数据集是一个著名的统计学资料,被机器学习研究人员大量使用。它包含了150组实例,4种生物特征和每组实例对应的鸢尾花种类...
这符合一些公司的小需求,实际代码量的统计工作 效果如图 代码如下: #coding:utf-8 import os,re #代码所在目录 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件,统计其中的代码...
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 ...
2、统计 app 最活跃日期及当前使用量。 3、统计 app 最活跃时间段及每个时间段使用情况。 4、分析 app 最活跃地区及该地区范围使用量情况。 5、将使用量最高的 app 统计显示其类别。 6、建立性别年龄预测模型并检验...
最近在用python做数据统计,这里总结了一些最近使用时查找和总结的一些小技巧,希望能帮助在做这方面时的一些童鞋。有些技巧是很平常的用法,平时我们没有注意,但是在特定场景,这些小方法还是能带来很大的帮助。 1...
数据可视化的教学代码: 包括折线图+柱状图+饼图+盒图+散点图+直方图+3d图 画布的布局、画图中中文乱码的解决、背景板的选择等 简单的数据分析和可视化案例: titanic数据的简单分析可视化 iris数据的聚类可视化 GDP...
'''进行ADF检验 adf_test的返回值 Test statistic:代表检验统计量 ...(4)看检验统计量和临界值,检验统计量小于临界值的话,就拒绝原假设,认为序列是平稳的;大于的话,不能拒绝,认为是不平稳的
从绘制蟒蛇、理解天天向上的力量到机器学习、网络爬虫,从文本进度条、统计名著人物重要性到图像手绘效果、雷达图绘制,绝大多数实例为作者原创,将随着内容深入不断激发读者学习Python语言的热情,因为“编程是件很...