Python 实现代码行数统计

作者: Python高效编程 | 来源:发表于2019-04-20 22:54 被阅读5次

具体代码在微信公众号:Python高效编程后台回复2019420获取。

前面我们一直在介绍图形界面,这次换个口味,在命令行实现代码行数统计程序。本程序共 135 行,其中 18 行空行、110 行有效代码、7行注释,大小为 7.71 KB。

[图片上传失败...(image-f9cd-1555772066300)]

主要思路

通过 argparse 设置四个参数 -f, -p, -s, -r,其中 -f 表示单个 py 文件,-p 表示文件夹地址。-s 接排序的关键字,也就是根据哪个属性显示信息。默认情况下,信息按 total 关键字降序排序,我们可以通过加上 -r 参数来得到升序序列。

对于单个 Py 文件,我们首先以只读方式读取文件,按行读取。每读取一行,total + 1。如果该行长度为 0,blank + 1。如果以‘#’开头,note + 1。否则的话,code + 1。其中,如果遇到单引号、双引号或者三引号,程序要单独计算这些注释的行数,主循环直接跳过相应次数的迭代。最后,通过 format 字符串打印字符。其中,代码要以英文命名,否则会显示混乱。

解析参数

我们使用标准库中的 argparse 来设置命令行参数。首先新建一个 parser 对象,并设置相应的提示信息。通过 group 对象添加的参数互斥,也就是说 -p -f 参数不能同时使用。parse_args() 用来获取对象参数。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n63" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import argparse

def parse_args():
parser = argparse.ArgumentParser(usage='命令行统计代码函数', description='统计文件夹或者单个文件信息')
group = parser.add_mutually_exclusive_group()
group.add_argument('-p', '--path', help='输入你的文件地址', default='')
group.add_argument('-f', '--file', help='输入你的文件名', default='')
parser.add_argument('-s', '--sort', help='排序', default='total')
parser.add_argument('-r', '--reverse', help='逆序:不需要参数', action='store_false', default=True)
args = parser.parse_args()
return args</pre>

基本判断

简单的判断,有三个分支。长度为 0,blank + 1;以‘#’开头,note + 1。如果既不是空行,也不是注释,那么代码的行数 + 1。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if not len(line):
blank += 1
elif line.startswith('#'):
note += 1
else:
code += 1</pre>

注释判断

对于以引号开头的行,我们就要小心了。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n69" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">elif line.startswith('"' or '"""' or "'" or "'''"):
if line.startswith('"""') or line.startswith("'''"):
temp_note = process_note(lines[num:], line[:3])
else:
temp_note = process_note(lines[num:], line[0])
note += temp_note</pre>

我们需要 process_note 函数计算这些引号所占的行数。从引号开始的那一行计数,一直都引号结束的地方停止计数,最后返回注释的行数。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n71" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 总行数 空行数 代码行 注释行 文件大小
def process_note(lines, symbol):
note = 0
for line in lines:
note += 1
line = line.strip()
if line.endswith(symbol):
break
return note</pre>

与之配合的代码如下。对于引号所占的行,我们可以直接跳过,继续执行 for 循环。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n73" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if temp_note > 1:
temp_note -= 1
continue</pre>

要点就梳理到这里了,具体代码在微信公众号:Python高效编程后台回复2019420获取。

写文不易,还请大家多多转发支持。

相关文章

  • 实现python中的代码行数统计思路:

    实现python中的代码行数统计思路: 统计文件中代码的总行数减去空行单行注释以及多行注释功能: 1.获取文件内容...

  • python实现的源码行数统计源码

    把代码过程比较常用的内容做个备份,下面的代码是关于python实现的行数统计的代码。 ''' Author:...

  • Python 实现代码行数统计

    具体代码在微信公众号:Python高效编程后台回复2019420获取。 前面我们一直在介绍图形界面,这次换个口味,...

  • Python 练习小工具,统计代码行数

    Python 练习小工具,统计代码行数。Java, C, Object-C, Swift

  • Python实现代码行数统计工具

    转自:Python中文社区我们经常想要统计项目的代码行数,但是如果想统计功能比较完善可能就不是那么简单了, 今天我...

  • 统计学--感知机

    参考李航的统计学习 感知机学习算法 Python实现感知机代码 Python代码实现对偶形式

  • find 命令的妙用

    1.统计代码行数命令 统计 src 目录下所有 .js 文件的代码行数 wc 命令返回结果[行数] [词数] [字...

  • GIT统计代码量

    GIT统计代码量 Git统计个人提交代码行数 Git统计项目总行数 查看git上个人代码量(需要修改usernam...

  • 统计代码行数

    eg: 比如统计js文件的行数:find demo/ -name "*.js" |xargs cat|wc -l ...

  • 统计代码行数

    b[^:b#/]+.$ 需要做一个大致的统计代码行数,搜了一下代码统计工具,都比较麻烦,需要第三方的工具和一些配置...

网友评论

    本文标题:Python 实现代码行数统计

    本文链接:https://www.haomeiwen.com/subject/bqprgqtx.html