Python实现wc.exe
项目相关要求
- 基本功能
-c file.c 返回文件file.c的字符数 (实现)
-w file.c 返回文件file.c的词的数目(实现)
-l file.c 返回文件file.c的行数(实现) - 扩展功能
-s 递归处理目录下符合条件的文件(实现)
-a 返回更复杂的数据(代码行 / 空行 / 注释行)(实现)
文件名支持通配符(*, ?)(实现) - 高级功能
-x 程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。(未实现)
解题思路
刚看到这个题目的时候觉得这个项目的主要内容没有什么难度,但是对于扩展功能中的代码行、空行、注释行的含义理解不是很懂,仔细看了几次文档和反复用IDE进行验证才搞懂。考虑到项目主要是对文件的IO和对String的解析,所以选择了开发效率较高的python。但由于以前从来没有开发过使用命令行参数启动的程序,所以查阅了许多网站后最终选择了argparse模块解析命令行参数。
设计实现过程和代码说明
设计思路:
以main()为中心,通过command_parse()获取操作与参数,根据是否查询子目录调用get_file_recursive()和get_file()获取需要遍历的文件list,最后根据相应的操作对相应的文件使用相应的get_?()方法。
在main()中调用command_parse()解析命令行参数并返回,使用argparse模块
def command_parse():
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true', default=False, help='计算文件的字符数')
parser.add_argument('-w', action='store_true', default=False, help='计算文件词的数目')
parser.add_argument('-l', action='store_true', default=False, help='计算文件的行数')
parser.add_argument('-s', action='store_true', default=False, help='递归处理目录下符合条件的文件')
parser.add_argument('-a', action='store_true', default=False, help='返回更复杂的数据(代码行 / 空行 / 注释行)')
parser.add_argument(action="store", dest="file")
args = parser.parse_args()
file_path = args.file
# 通过往字符串中加入各操作的字母传递信息
param = ''
if args.c:
param = param + 'c'
if args.w:
param = param + 'w'
if args.l:
param = param + 'l'
if args.s:
param = param + 's'
if args.a:
param = param + 'a'
# 如果输入的是相对路径,自动补充当前路径
if "\\" not in file_path:
file_path = os.path.abspath(os.path.join(os.getcwd(), "..")) + "\\" + file_path
return param, file_path
根据有无-s参数决定是否递归子目录和文件(调用get_file_recursive()和get_file()),并使用正则表达式处理通配符(*, ?)的问题
# 如果有通配符,识别符合的目录,返回符合的文件路径
if "*" in file_path or "?" in file_path:
pattern = r"^" + file_path.split("\\")[-1].replace("*", "[0-9a-zA-Z]*").replace("?", "[0-9a-zA-Z]*") + "$"
# 获取根路径
root = os.path.abspath(os.path.join(file_path, ".."))
file_list = get_file_recursive(root, pattern)
# 没有通配符,先检测路径是否正确,然后递归
else:
if not os.path.isdir(file_path):
print("输入的路径不是目录!")
print("ERROR: " + file_path)
exit()
# 无通配符,匹配任意字符
file_list = get_file_recursive(file_path, "[\s\S]*")
查找需要查询的filelist
def get_file_recursive(root, pattern):
file_list = []
dir_list = os.listdir(root)
for i in range(len(dir_list)):
path = os.path.join(root, dir_list[i])
if os.path.isdir(path):
file_list.extend(get_file_recursive(path, pattern))
elif os.path.isfile(path) and re.match(pattern, dir_list[i]):
file_list.append(path)
return file_list
基本功能:
def get_chars(file):
with open(file, 'r') as f:
data = f.read()
print("文件(" + file + ")的字符数: " + str(len(data)))
def get_words(file):
with open(file, 'r') as f:
data = f.read()
# 将所有不是英文的字符replace成空格,使用split查询list长度即可
data = re.sub('[^a-zA-Z]', '', data)
print("文件(" + file + ")的词的数目: " + str(len(data.split())))
def get_lines(file):
with open(file, 'r') as f:
data = f.read()
print("文件(" + file + ")的行数: " + str(len(data.split("\n"))))
扩展功能的-a:
def get_appends(file):
with open(file, 'r') as f:
data = f.read()
empty = 0
code = 0
annotation = 0
# 标识多行注释
is_annotation = False
for line in data.split('\n'):
if is_annotation:
annotation = annotation + 1
if '*/' in line:
is_annotation = False
continue
# 去除空格等无用字符
visual_line = line.replace('\t', '').replace(' ', '')
if len(visual_line) < 2:
empty = empty + 1
elif '/*' in visual_line:
annotation = annotation + 1
is_annotation = True
elif '//' in visual_line:
annotation = annotation + 1
else:
code = code + 1
print("文件(" + file + ")的空行数: " + str(empty))
print("文件(" + file + ")的代码行数: " + str(code))
print("文件(" + file + ")的注释行数: " + str(annotation))
测试运行
空文件测试:

只有一个字符的文件:

只有一个词的文件:

只有一行的文件:

一个典型的源文件:

-a 命令和通配符测试:

代码覆盖率:

PSP
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 30 | 30 |
| · Estimate | · 估计这个任务需要多少时间 | 30 | 30 |
| Development | 开发 | 410 | 700 |
| · Analysis | · 需求分析 (包括学习新技术) | 30 | 60 |
| · Design Spec | · 生成设计文档 | 30 | 20 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 15 | 10 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 15 | 10 |
| · Design | · 具体设计 | 120 | 150 |
| · Coding | · 具体编码 | 120 | 300 |
| · Code Review | · 代码复审 | 20 | 30 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 60 | 120 |
| Reporting | 报告 | 65 | 85 |
| · Test Report | · 测试报告 | 20 | 40 |
| · Size Measurement | · 计算工作量 | 15 | 15 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 30 |
| 合计 | 505 | 815 |
项目小结
从PSP表可以看出我在拿到这个题目之后低估了题目的难度,没有在计划和需求分析上花太多的时候,文档也没有太仔细看,所以开发前的设计文档不能很好的完成需求,导致后来基本处于一种边写代码边改结构的地步,开发效率大大降低,也因为经常改动代码所以bug很多。当然,也和自己开发时不是特别专注有关(边做其他事情边开发)
这次的项目让我了解到自己对于python并不是特别的熟练,对正则表达式的不熟悉也让我吃了很多亏。总体来说,这次经历受益匪浅,让我了解到了自己的不足,也让我知道做项目不能太急,要按流程合理地分配时间才能真正地提高开发效率和代码质量。
Python实现wc.exe的更多相关文章
- Python模拟wc命令(软件测试第二次作业)
Python实现字符,单词,行,代码行,空行及可视化 Gitee项目地址:https://gitee.com/biubiubiuLYQ/word_and_character_statistics 一. ...
- 软工作业No.1。Java实现WC.exe
网址:https://github.com/a249970271/WC WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有w ...
- 通过python实现wc基本功能
---恢复内容开始--- 1.Github项目地址: https://github.com/zhg1998/ww/blob/master/wc.py 2.项目相关要求: 写一个命令行程序,模仿已有wc ...
- python脚本生成exe可执行文件
1.先安装第三方插件: py2exe. Get py2exe from http://www.py2exe.org/ 在download里下载与自己python对应的版本 2.写一个测试python文 ...
- 打包python脚本为exe可执行文件-pyinstaller和cx_freeze示例
本文介绍使用cx_freeze和pyinstaller打包python脚本为exe文件 cx_freeze的使用实例 需要使用到的文件wxapp.py, read_file.py, setup.py ...
- python打包成exe
目前有三种方法可以实现python打包成exe,分别为 py2exe Pyinstaller cx_Freeze 其中没有一个是完美的 1.py2exe的话不支持egg类型的python库 2.Pyi ...
- WC.exe【C】
gitee传送门!!!(电脑打不开github,多次尝试未果,决定先用gitee存着先) 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序 ...
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
- 模仿WC.exe的功能实现--node.js
Github项目地址:https://github.com/102derLinmenmin/myWc WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要 ...
随机推荐
- Python学习笔记之selenium 定制启动 chrome 的选项
在自动化中,默认情况下我们打开的就是一个普通的纯净的chrome浏览器,而我们平时在使用浏览器时,经常就添加一些插件,扩展,代理之类的应用.所以使用 selenium 时,我们可能需要对 chrome ...
- php判断是否为ajax请求
先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以 ...
- 010. windows10下安装kivy 1.9.1版
Microsoft Windows [版本 10.0.14393] 以管理员权限打开cmd (c) 2016 Microsoft Corporation. 保留所有权利. 1. C:\Users\LG ...
- Docker -- resolve "join node timeout" error
在worker节点机器上通过docker swarm join 到 manger node时,报Timeout错误,如下: Error response from daemon: Timeout wa ...
- VS配置附加包含目录技巧
把include文件夹(里面是某个库的头文件)拷到自己的项目中,添加头文件时需要使用#include"include\xxx.h"方式,如果打算使用#include"xx ...
- oracle误删数据的解决方法
之前不小心误删了一条数据,索性我还记得id,通过select * from 表名 as of timestamp to_timestamp('2017-6-23 9:10:00','yyyy-mm-d ...
- sendCloud群发邮件一点总结
1.群发时,若发送的邮件为html页面,则不能用[普通发送]然后foreach循环: 若是单纯的文本,则可以用普通发送,否则,第一封邮件成功后,后面的都是html乱码. 2.若要用html模板发送,可 ...
- apk安装包信息
String archiveFilePath="sdcard/DangDang.apk";//安装包路径 PackageManager pm = getPacka ...
- codeforce468DIV2——D. Peculiar apple-tree
题意给你一颗树,开始时每个结点都有一个小球,每一秒钟每个小球都往上滚一层,当两个球在同一个结点的时候会被消去,如果三个五个七个等在同一个结点的化消去后只剩一个. 分析 这对我来说就TM是英语阅读理解哇 ...
- 十二.filter
Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是 ...