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万多行.那我们还是进入下一项吧. 界面搭建我 ...
随机推荐
- 【美妙的Python之二】Python初步
美妙的Python之Python起步 简而言之: Python 是能你无限惊喜的语言,与众不同. 1.动态类型: Python是一种动态类型语言,不 ...
- cocos2d-x 事件分发机制 ——加速计事件监听
加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...
- 【转】SVN:Android Studio设置忽略文件
Android Studio创建的Android项目一般需要忽略 参考: http://blog.csdn.net/qq_22780533/article/details/51965007 1..id ...
- Visual Studio Package 插件开发
背景 这段时间公司新做了一个支付系统,里面有N个后台服务,每次有更新修改,拷贝打包发布包"不亦乐乎"...于是我想要不要自己定制个打包插件. 部分朋友可能会认为,有现成的可以去找一 ...
- 查看LINUX发行商版本与LINUX内核版本
查看LINUX发行商版本:[root@server-mysql ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6.3 (Sant ...
- Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果
开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...
- 在Silverlight中使用async/await
现在 async/await 大行其道,确实,有了 async/await ,异步编程真是简单多了,个人觉得 async/await 的出现,给开发者还来的方便,绝不亚于当年 linq 的出现. 但要 ...
- ubuntu15.10下sublime text3 无法输入中文解决办法
原文链接:http://geek.csdn.net/news/detail/44464 1.首先保证你的电脑有c++编译环境 如果没有,通过以下指令安装 sudo apt-get install bu ...
- ui-router的使用
使用时需要ui中用ui-view指令指定 如: <div ui-view></div> 首先配置注册 ui-route var mainModule = angular.mod ...
- js中 的这些距离你知道吗?