[python]写一个统计代码行数的codeCounter 推荐
2013-03-31 16:01
645 查看
某一天过去SY那儿,突发奇想说要写一个统计代码行数的小程序。说干就干,约定了一个时间——周六,来把这个想法给实现了。当然这个项目人家做过的也未必,google一下,果然有非常优秀的win下面的代码统计工具sourceCounter。当然我们是用python来写,确定了数据结构和算法之后,我们就开始实现了。
我们要实现的是一个能够遍历指定的文件夹或文件,数出有多少行,然后好好将这个信息放到树的节点里面。总之,这是一个N叉树,N取决于该目录下面,有多少个子目录或子文件。我们使用列表list来模拟树型结构:
parentDir
subDir
在数据结构里面就是 [{counter: co, name: parentDir}, [{couter:co2, name:subDir}[]]]。当然这是最简单的情况,复杂一点就是,很多层嵌套,视目录深度而决定。总之就是发现了一个文件或者目录,就递归地扩展下去。上代码:
这就是整个算法的核心部分,用os.walk整个dir,得到一棵树。一开始我们提出了两种方案:一,先生成树,再统计代码,因为需要回填;二,在生成树的同时,计算出代码行数。SY很敏捷,一下自己想出直接在后面加上folder_tree[0]['counter'] = folder_tree[0]['counter'] + sub_folder_tree[0]['counter']. 也就是每次遍历完一棵子树,就更新父节点的counter数据。这其中使用了递归的原理。code complete里面说,递归不是用来求求阶乘和斐波那契数列的,而是需要使用在更有效的需要栈的地方,我想说,这里就是。其核心原理就是每一层,构造一个[{counter:co, name:na }[]], 插入到父节点的[]中,构造树的过程,也完成了有关的计算。
接下来就是实现驱动脚手架了(一些数据输入和函数调用),然后实现打印模块,打印模块,需要表现缩进,故而有一个参数是tabs次数。具体实现如下:
一切就是这么简单,然后就定义一下有哪些文件类型了,这个可以用正则表达式来完成:
很显然是普通文件名,如果要加入下划线则在把字符串改成‘[a-zA-Z1-9]+[a-zA-Z1-9_.]* .(py|c|java省略’。
最后,如果想要把树打印到文件里面也是可以的,之需要在print前面加上>>log_file变成 print >>log_file。总体代码不超过100行。当然使用linux下面的shell一句就能搞定:
当然这其实是linux应用程序的组合使用,grep等等强大的工具,表示还没有到那个神级别。好吧,窗外的雨下个不停,可是我的手指敲击键盘的声音,却要渐渐停息了。
git代码: https://github.com/bibodeng/pyCodeCounter/blob/master/codeCounter.py
by bibodeng 2013-3-31
我们要实现的是一个能够遍历指定的文件夹或文件,数出有多少行,然后好好将这个信息放到树的节点里面。总之,这是一个N叉树,N取决于该目录下面,有多少个子目录或子文件。我们使用列表list来模拟树型结构:
parentDir
subDir
在数据结构里面就是 [{counter: co, name: parentDir}, [{couter:co2, name:subDir}[]]]。当然这是最简单的情况,复杂一点就是,很多层嵌套,视目录深度而决定。总之就是发现了一个文件或者目录,就递归地扩展下去。上代码:
def walk_dir(self,folder_tree, topdown=True): for root, dirs, files in os.walk(folder_tree[0]['name'], topdown): for name in files: if name[0] == '.': continue if self.my_pattern.match(name): path_name = os.path.join(root,name) if os.path.islink(path_name): continue # calculate the summry of the file count_result = self.count_code_of_file(path_name) # insert into the tree sub_folder_tree = [{'name':path_name,'counter': count_result},[]] folder_tree[1].append(sub_folder_tree) # sum folder_tree[0]['counter'] = folder_tree[0]['counter'] + count_result for name in dirs: if name[0] == '.': continue path_name = os.path.join(root,name) # print(path_name) sub_folder_tree = [{'name':path_name,'counter': 0},[]] folder_tree[1].append(sub_folder_tree) self.walk_dir(sub_folder_tree, topdown) # sum folder_tree[0]['counter'] = folder_tree[0]['counter'] + sub_folder_tree[0]['counter'] return
这就是整个算法的核心部分,用os.walk整个dir,得到一棵树。一开始我们提出了两种方案:一,先生成树,再统计代码,因为需要回填;二,在生成树的同时,计算出代码行数。SY很敏捷,一下自己想出直接在后面加上folder_tree[0]['counter'] = folder_tree[0]['counter'] + sub_folder_tree[0]['counter']. 也就是每次遍历完一棵子树,就更新父节点的counter数据。这其中使用了递归的原理。code complete里面说,递归不是用来求求阶乘和斐波那契数列的,而是需要使用在更有效的需要栈的地方,我想说,这里就是。其核心原理就是每一层,构造一个[{counter:co, name:na }[]], 插入到父节点的[]中,构造树的过程,也完成了有关的计算。
接下来就是实现驱动脚手架了(一些数据输入和函数调用),然后实现打印模块,打印模块,需要表现缩进,故而有一个参数是tabs次数。具体实现如下:
def print_tree(self,folder_tree, tabs = 0,topdown=True): if os.path.isdir(folder_tree[0]['name']): print ' ' print ' '*tabs,'+',folder_tree[0]['name'] , ' : ' ,folder_tree[0]['counter'] else: file_name = os.path.split(folder_tree[0]['name']) print ' '*tabs,'+',file_name[1], ' : ' ,folder_tree[0]['counter'] return if folder_tree[1]: for sub_tree in folder_tree[1]: self.print_tree(sub_tree,tabs +1) else: return
一切就是这么简单,然后就定义一下有哪些文件类型了,这个可以用正则表达式来完成:
my_pattern = re.compile(r'[a-zA-Z1-9]+.(py|c|java|php|cpp|css|html|xml|htm|js|cs|h|asm|sh|ruby|perl)$')
很显然是普通文件名,如果要加入下划线则在把字符串改成‘[a-zA-Z1-9]+[a-zA-Z1-9_.]* .(py|c|java省略’。
最后,如果想要把树打印到文件里面也是可以的,之需要在print前面加上>>log_file变成 print >>log_file。总体代码不超过100行。当然使用linux下面的shell一句就能搞定:
find /a -name "*.c" |xargs cat|grep -v ^$|wc -l
当然这其实是linux应用程序的组合使用,grep等等强大的工具,表示还没有到那个神级别。好吧,窗外的雨下个不停,可是我的手指敲击键盘的声音,却要渐渐停息了。
git代码: https://github.com/bibodeng/pyCodeCounter/blob/master/codeCounter.py
by bibodeng 2013-3-31
相关文章推荐
- python 统计一个目录下代码的行数、空行和注释
- python 统计代码行数,注释行数的简单实现
- Python:统计特定文件夹中,特定文件代码行数的小程序
- python代码统计大文本文件行数
- 一个统计代码行数的简单方法
- python实现统计代码行数的方法
- Python-统计svn变更代码行数
- 用java统计一个目录(含子目录)下所有java源文件中代码行数
- 一个简单的统计C源程序代码行数的小程序
- 一个统计代码行数的小工具(附源代码)
- C++ 一个统计文件夹下所有代码文件行数的小工具
- python 统计代码行数简单实例
- 使用python写的代码行数统计程序
- 《Python Case系列》一.Python实现统计项目的代码行数
- Python脚本实现代码行数统计代码分享
- 一个统计PHP代码行数的小代码
- (python) 统计代码行数
- 分享一个用于统计MATLAB代码行数的小程序
- 一个简单的统计代码行数的程序(基于MFC)
- 第0007道练习题_Python统计代码行数注释行数空白行数