github address:https://github.com/gdutlzk/wc/blob/my_new_branch1/wordcount(1).py
项目要求:wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:
wc.exe [parameter] [file_name]
基本功能列表:
wc.exe -c file.c //返回文件 file.c 的字符数 (已实现)
wc.exe -w file.c //返回文件 file.c 的词的数目 (已实现)
wc.exe -l file.c //返回文件 file.c 的行数 (已实现)
额外功能:(未实现)
-h 显示帮助命令
-g 将输出的结果用pygal模块绘制svg图像
扩展功能:
-s 递归处理目录下符合条件的文件。(未实现,递归函数出现困难)
-a 返回更复杂的数据(代码行 / 空行 / 注释行)。(已实现)
[file_name]: 文件或目录名,可以处理一般通配符。(未实现)
高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。(未实现)
解题思路:
使用os模块来判断输入路径为文件或目录
尝试使用模块sys来获取命令,结果失败了
文件处理方面使用了os模块的部分函数,例如:os.path.join(),os.listdir()等,还有使用了open(),readlines()等基础函数
寻找资料:大部分是再网上查询,然后请教同学和看书
设计实现过程:
wordcount(file)方法打开文件,并且可以返回基本的数值,例如字符数,单词数和行数
linecount(path)方法,通过路径打开文件,返回各种行数的数目
command(skr, path)方法,通过输入,获取命令,再根据命令的字符来确定执行什么功能
search(path, ci)方法,本设计为递归处理目录下符合条件的文件,结果未实现
代码说明:
计算字符数,单词数和行数:
1 def wordcount(file): 2 chars = 0 3 word = [] #单词列表 4 lines = 0 5 for line in file: 6 chars += len(line) 7 line = line.replace("[^0-9a-zA-Z\\u4e00-\\u9fff]", " ") #正则表达式替代非英文、汉字、非数字 8 line = line.strip() #消除左右空格 9 lb = line.split(" ") #以空格分割 10 word.extend(lb) 11 words = len(word) 12 lines += 1 13 return chars , words , lines
计算具体的行数:
1 1 def linecount(path): 2 2 blank, comments, codelines, totalines, begin_comment, long_comment= 0, 0, 0, 0, 0, 0 3 3 f_list = open(path).readlines() 4 4 for line in f_list: 5 5 totalines += 1 6 6 if line.strip().startswith('#'): #于检查字符串是否是以#开头,是返回TRUE 7 7 comments += 1 8 8 elif line.strip().startswith("'''") or line.strip().startswith('"""'): 9 9 if begin_comment == 0: 10 10 long_comment += 1 11 11 begin_comment = 1 12 12 else : 13 13 begin_comment = 0 14 14 comments += long_comment 15 15 elif len(line) <= 1: 16 16 blank += 1 17 17 else: 18 18 codelines += 1 19 19 return totalines, comments, codelines, blank
获得条件下的目录文件:
1 search(path, ci): 2 for filename in os.listdir(path): 3 fp = os.path.join(path, filename) 4 if os.path.isfile(fp) and ci in filename: #如果路径fp是一个存在的文件,返回True。 5 command(-q, fp) 6 elif os.path.isdir(fp): #如果fp是一个存在的目录,则返回True 7 search(fp, ci) #递归处理
获取命令并执行程序:
command(skr, path): file = open(path, "r") chars, words, lines = wordcount(file) totalines, comments, codelines, blank = linecount(path) if skr == '-c': print('字符数', chars) elif skr == '-w': print('单词数', words) elif skr == '-l': print('行数', lines) elif skr == '-s': search(sys.argv[1]) elif skr == '-a': print('空格行', blank) print('代码行', codelines) print('注释行', comments) #if len(sys.argv) < 4: #print(sys.argv) """if sys.argv[1] != "-c" and sys.argv[1] != "-w" and sys.argv[1] != "-l": print("参数出现错误") else: skr = sys.argv[1] path = sys.argv[2] elif len(sys.argv) == 4: if sys.argv[2] != "-c" and sys.argv[2] != "-w" and sys.argv[2] != "-l" and sys.argv[2] != "-a": print("参数出现错误") elif sys.argv[1] != "-s": print("参数出现错误") else: skr = sys.argv[2] path = sys.argv[3]""" print("please input:") skr,path = input().split() command(skr, path)
程序运行简单截图:
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
60 | 60 |
· Estimate |
· 估计这个任务需要多少时间 |
60 | 30 |
Development |
开发 |
240 | 360 |
· Analysis |
· 需求分析 (包括学习新技术) |
40 | 120 |
· Design Spec |
· 生成设计文档 |
40 | 60 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
50 | 30 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 | 50 |
· Design |
· 具体设计 |
50 | 40 |
· Coding |
· 具体编码 |
180 | 270 |
· Code Review |
· 代码复审 |
45 | 80 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
40 | 45 |
Reporting |
报告 |
25 | 80 |
· Test Report |
· 测试报告 |
30 | 25 |
· Size Measurement |
· 计算工作量 |
15 | 20 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划
|
25 | 20 |
合计 |
890 | 1290
|
项目总结:
本次使用python来实现这个命令行程序,而python本身非常适合处理这方面的作业,虽然在期间遇到了不少的困难,例如os模块运用的不熟练和代码本身的逻辑性有一些问题,但是经过了一些努力后,也算是勉强解决了这些问题。而在其中,我发现了我对递归函数的使用的不熟练和对条件测试的不理解的这些缺点,这些也成为了我这次作业极大的绊脚石。我希望能够在接下来的时间里,努力提高自己,以完成这些作业的目的。