小白&python实现wc基本功能&扩展功能

 

项目github地址https://github.com/linlixina/wc

 

 

项目实现功能:

基本功能列表

  1. wordCounter.exe -c file.c //返回文件 file.c 的字符数
  2. wordCounter.exe -w file.c //返回文件 file.c 的词的数目
  3. wordCounter.exe -l file.c //返回文件 file.c 的行数

扩展功能

  1. wordCounter.exe -s file //递归处理目录下符合条件的文件。
  2. wordCounter.exe -a file.c //返回更复杂的数据(代码行 / 空行 / 注释行)。

    空行: 本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如`“{”`。

    代码行:本行包括多于一个字符的代码。

    注释行: 本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:

        `} // 注释`,在这种情况下,这一行属于注释行。

 

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 90  90

· Estimate

· 估计这个任务需要多少时间

90  90

Development

开发

365  385

· Analysis

· 需求分析 (包括学习新技术)

 240  240

· Design Spec

· 生成设计文档

 20  20

· Design Review

· 设计复审 (和同事审核设计文档)

 30  30

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 15  15

· Design

· 具体设计

180 180

· Coding

· 具体编码

 180  200

· Code Review

· 代码复审

 120  120

· Test

· 测试(自我测试,修改代码,提交修改)

 180  180

Reporting

报告

 165  165

· Test Report

· 测试报告

 120  120

· Size Measurement

· 计算工作量

 15  15

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 30  30

合计

   620  640

 

关键代码:

argparse模块的调用:

 1 from optparse import OptionParser
 2 parser = OptionParser()
 3 parser.add_option("-l", "--line",
 4                   dest="lines",
 5                   action="store_true",
 6                   default=False,
 7                   help="count lines")
 8 parser.add_option("-w", "--word",
 9                   dest="words",
10                   action="store_true",
11                   default=False,
12                   help="count words")
13 parser.add_option("-c", "--char",
14                   dest="chars",
15                   action="store_true",
16                   default=False,
17                   help="count chars")
18 parser.add_option("-a", "--aaa",
19                   dest="aaas",
20                   action="store_true",
21                   default=False,
22                   help="count aaas")
23 parser.add_option("-s", "--subject",
24                   dest="subjects",
25                   action="store_true",
26                   default=False,)
27 options, args = parser.parse_args()

主要函数,对读取文件进行处理:

 1 def read_message(_files):
 2     for file in _files:
 3         f = judge(file)
 4         if f:
 5             with open(file, encoding="ISO-8859-1") as files:
 6                 cs = files.read()
 7                 chars = len(cs)
 8                 words = len(cs.split())
 9                 files.close()
10             with open(file, encoding="ISO-8859-1") as files:
11                 ls = files.readlines()
12                 lines = len(ls)
13                 files.close()
14             with open(file, encoding="ISO-8859-1") as files:
15                 l_ines = files.readlines()
16                 codelines, emptylines, commentlines = [], [], []  # 将满足条件的特殊行加入对应的列表中
17                 for line in l_ines:
18                     tmpline = line.replace(' ', '')
19                     tmpline = tmpline.replace('\t', '')
20                     tmpline = tmpline.replace('\n', '')
21                     if len(tmpline) == 1 or len(tmpline) == 0:  # 判断是否为空行
22                         emptylines.append(line)
23                     elif tmpline.startswith('//'):  # 判断是否为注释行
24                         commentlines.append(line)
25                     else:
26                         codelines.append(line)
27                 codelines = len(codelines)  # 通过列表长度判断特殊行行数
28                 emptylines = len(emptylines)
29                 commentlines = len(commentlines)
30                 wc_print(lines, words, chars, codelines, emptylines, commentlines)
31                 print(file)
32         else:
33             continue
根据不同的指令输出不同的值:
def wc_print(lines, words, chars, codelines , emptylines ,commentlines):
    if options.lines:
        print('行数:' + str(lines)),
    if options.words:
        print('词数:' + str(words)),
    if options.chars:
        print('字符数:' + str(chars)),
    if options.aaas:
        print('代码行/空行/注释行:' + str(codelines) + '/' + str(emptylines) + '/' + str(commentlines)),
    if options.subjects:
        print('行数:' + str(lines)),
        print('词数:' + str(words)),
        print('字符数:' + str(chars)),
        print('代码行/空行/注释行:' + str(codelines) + '/' + str(emptylines) + '/' + str(commentlines)),

运行结果示例:

过程中遇到的问题

       1.对argparse模块不熟悉;

       2.打开文件的编码问题:刚开始默认gpk无法识别。后来换utf-8还不行,最后百度到转为'ISO-8859-1',问题解决。

posted @ 2018-09-13 23:39  信安二参谋长  阅读(332)  评论(0编辑  收藏  举报