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万多行.那我们还是进入下一项吧. 界面搭建我 ...
随机推荐
- jQuery CSS 的操作函数
jQuery CSS 操作函数 下面列出的这些方法设置或返回元素的 CSS 相关属性. CSS 属性 描述 css() 设置或返回匹配元素的样式属性. height() 设置或返回匹配元素的高度. o ...
- wDatePicker使用说明文档
版权声明:本文为博主原创文章,未经博主允许不得转载. http://www.my97.net/dp/demo/ 4.5更新的内容 [重构]对WdatePicker.js做了较大规模的调整 [改进]自动 ...
- 李炎恢bootstarp_项目实战__瓢城企业(注释+源码)
源代码下载地址:http://pan.baidu.com/s/1gfI9Pj9 /********************************* pc界面设备页面***************** ...
- Android Studio 1.0.2 设置内存大小
http://www.linuxidc.com/Linux/2015-04/116457.htm Android studio 1.0.2默认最大内存是750M,这样跑起来非常的卡,难以忍受,机器又不 ...
- cocos2dx 以子弹飞行为例解说拖尾效果类CCMotionStreak
在游戏开发中,有时会须要在某个游戏对象上的运动轨迹上实现渐隐效果.比方子弹的运动轨迹,假设不借助引擎的帮助,这样的效果则须要通过大量的图片来实现.而Cocos2D-x的拖动渐隐效果类CCMotionS ...
- [Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin
Often, you have dependencies which you rarely change. In these cases, you can leverage the CommonsCh ...
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...
- PreferenceActivity的使用
PreferenceActivity是一个非常有用的基类,当我们开发Android项目时避免不了选项设置,这些设置习惯用Preference来保存.Android专门为这种Activity提供了便捷的 ...
- CentOS 6.7 安装配置BT下载工具Transmission
1.配置额外yum源 i386 cd /etc/yum.repos.d/ wget http://geekery.altervista.org/geekery-el6-i686.repo x86_64 ...
- bootstrap-datetimepicker 时间表箭头不能显示
我使用的是bootstrap-datetimepicker+bootstrap v3,但这个插件使用的时候,并没有和V3相匹配,仍然调用的是bootstrap V2的图标,代码是: <i cla ...