Python 统计代码行
正在学习 Python, 做了个统计代码行的功能,
参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能,
只是windows下测试了,而且代码文件编码形式是 utf-8的。
如果使用其它编码形式的话,估计修改下代码就行了。
功能特点:
是否统计空行
统计注释
设置忽略文件平
设置统计文件类型
根据不同文件类型,设置注释标签
以下,是代码:
# Created by Aaron <xinlingever@outlook.com> in 2014
# This code is for Python 3.4 import os, sys
import traceback def strToBool(v):
return v.lower() in ("yes", "true", "t", "") exts = ['.js', '.html', '.css', '.h', 'cpp']
commentTags = ['//,/* */','<!-- -->','/* */', '//, /* */', '//, /* */','//, /* */']
commentTypeIndex = ['.js', '.html', '.css', '.cs', '.cpp', '.h']
ignor_folders = ['debug', 'release', 'ipch', 'output', '.svn', '.git', 'durango', 'bld', 'layout']
max_line_limit = 2000 count_empty_line = False
if len(sys.argv) > 1:
here = sys.argv[1]
else:
here = sys.argv[0] if(len(sys.argv) > 2):
exts = sys.argv[2].split()
if(len(sys.argv) > 3):
count_empty_line = strToBool(sys.argv[3]) def read_line_count(fname):
count = 0
comment_count = 0 is_in_multi_comment = False
multi_line_comment_tag_tailer = ''
for line in open(fname, encoding='utf-8').readlines():
if count_empty_line and len(line.strip()) == 0:
count += 1
else:
if len(line.strip()) > 0:
count += 1 # count comment
if(is_in_multi_comment):
comment_count += 1 if(line.find(multi_line_comment_tag_tailer) >= 0):
is_in_multi_comment = False
else:
ext = (fname[fname.rindex('.'):]).lower()
if ext not in commentTypeIndex:
continue for commentTag in commentTags[commentTypeIndex.index(ext)].split(','):
if(len(commentTag.split()) == 1):
# single line comment
if line.strip().startswith(commentTag):
comment_count += 1
else:
if(line.find(commentTag) >= 0):
comment_count += 1 else:
# multi line comment
multi_line_comment_tags = commentTag.split()
multi_line_comment_tag_header = multi_line_comment_tags[0]
multi_line_comment_tag_tailer = multi_line_comment_tags[1] if line.find(multi_line_comment_tag_header) >= 0:
comment_count += 1
is_in_multi_comment = True
if line.find(multi_line_comment_tag_tailer) >= 0:
is_in_multi_comment = False return count,comment_count
if __name__ == '__main__':
line_count = 0
file_count = 0
comment_line_count = 0 subFolderCount = 0;
for base, dirs, files in os.walk(here):
for file in files:
#print(file)
# Check the sub directorys
if file.find('.') < 0:
#print(file)
continue ext = (file[file.rindex('.'):]).lower()
try:
if ext in exts:
path = os.path.join(base,file)
relative_path = path[len(here):].replace(file,'').lower()
is_ignore = False
for ignorFolder in ignor_folders:
if relative_path.find(ignorFolder) >= 0:
is_ignore = True
break;
if is_ignore:
continue c,c2 = read_line_count(path)
if max_line_limit > 0:
if c > max_line_limit:
continue file_count += 1 print ("\t.%s : %d %d" % (path[len(here):], c, c2))
line_count += c
comment_line_count += c2
#else:
#print(file, "is not in list")
except Exception as N:
print(traceback.format_exc())
pass
print ('File count : %d' % file_count)
print ('Line count : %d' % line_count)
print ('Comment line count : %d' % comment_line_count)
print ('Comment rage is {:.2%}'.format ( comment_line_count / line_count))
Python 统计代码行的更多相关文章
- Python入门之用Python统计代码行
Pycharm每天都要写很多代码,如何统计每天的代码行数呢?作为一个目标十万行的coder,要想想办法! 题目:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列 ...
- 利用python统计代码行
参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...
- python统计代码行数
以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考 参考链接: https://gist.git ...
- 007-使用python统计代码行数,空行以及注释
# 自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来 1.打开文件方法 1.1 以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 f ...
- python统计代码总行数(代码行、空行、注释行)
我们在工作或学习代码的过程中,经常会想知道自己写了多少行代码,今天在项目环境写了个脚本统计了项目代码的数量. 功能: 1.统计代码总行数 2.统计空行数 3.统计注释行数 # coding=utf-8 ...
- Eclipse统计代码行数
开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...
- 在Flash Builder或者Eclipse统计代码行数的方法
在Flash Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件
- 【Linux】常用命令-统计代码行数
公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了, 如何统计代码行数? 1,最先想到的肯定是 wc. wc -l *.h 将查看[当前目录]下头文件的代码行数,输出结 ...
- c#统计代码行数
小编,已经快学了两年编程了.昨天突发奇想,想统计下这些年到底写过多少行代码,于是做了一个这个小程序来统计代码行数.老规矩,先上图. 比较惭愧,写了两年只有2万多行.那我们还是进入下一项吧. 界面搭建我 ...
随机推荐
- SQL用replace替换文本部分内容
替换文本内容 update tk_question set stem=replace(cast(stem as varchar(max)) ,'被替换文本','替换文本') 查询字段内容长度 sele ...
- 【转载】NativeSQL实例
转自:http://blog.sina.com.cn/s/blog_3f2c03e301017fqz.html ---------------------------------------- ...
- 关于ABAP事件的一张图
事件: 这里有几组事件关键字 ,这些事件关键字在特定环境下控制ABAP/4 程序流. 逻辑数据库 是典型报表程序的外部流控制的中心点.如果将逻辑数据库链接到报表 程序,将导致显示选择 屏幕,并决定系统 ...
- 【MongoDB】The Access control of mongodb
In this blog we mainly talk about the access control including limitation of ip, setting listen port ...
- android intent 隐式意图和显示意图(activity跳转)
android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity B ...
- oracle2
为什么选择oracle--性能优越 概述:目前主流数据库包括 微软: sql server和access 瑞典MySql: AB公司mysql ibm公司: db2(处理海量) 美国Sybase公司: ...
- oracle7
管理初始化参数 管理初始化参数(调优的一个重要知识点,凭什么可以对数据库进行调优呢?是因为它可以对数据库的一些参数进行修改修正) 初始化参数用于设置实例或是数据库的特征.oracle9i提供了200多 ...
- 【转】iOS应用崩溃日志揭秘2
这篇文章还可以在这里找到 英语 场景 4: 吃棒棒糖时闪退! 用户邮件说, "当rage master吃棒棒糖时应用就闪退-" 另一用户说, "我让rage master ...
- 随便说说removeFromSuperview方法
之前写过一篇关于removeFromSuperview方法处理的文章,写完后一直就没怎么更新这篇文章.这两天回过头来看看,感觉这篇文章有些地方写的不够严谨,而且还有一些自己理解错的地方,所以打算重写这 ...
- VS2010项目转化为VS2008项目
第一步: 打开VS2010项目的SLN文件有如下代码: Microsoft Visual Studio Solution File, Format Version 11.00# Visual Stud ...