WC.exe python

Github 地址:https://github.com/lzhooo/ruanjiangongcheng_wc

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 20

 15

· Estimate

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

15

10

Development

开发

 300

280

· Analysis

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

60 

70

· Design Spec

· 生成设计文档

25 

25

· Design Review

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

25 

30 

· Coding Standard

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

15 

15

· Design

· 具体设计

45 

40

· Coding

· 具体编码

60 

60

· Code Review

· 代码复审

30 

30 

· Test

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

30 

60 

Reporting

报告

45 

45 

· Test Report

· 测试报告

30 

20 

· Size Measurement

· 计算工作量

10 

20 

· Postmortem & Process Improvement Plan

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

30 

10 

合计

 

740

730 

一、 功能说明:

  实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数。

  基本功能列表:

    wc.py -c file.c //返回文件 file.c 的字符数

    wc.py -w file.c //返回文件 file.c 的词的数目

    wc.py -l file.c //返回文件 file.c 的行数

  扩展功能列表:

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

二、解题思路:

  通过获取命令行中相对应的参数,获得文件目录/文件名以及处理命令。然后打开文件进行相关操作。

三、主要代码:

  CmdSpecialLine()用于统计特殊行的数目,返回dict字典格式

def CmdSpecialLine(filename):
    try:
        file = codecs.open(filename,'rb')
    except:
        print("文件打开失败,请确认输入是否正确")
        sys.exit(1)
    specialline = dict({
        'line_value':0,
        'line_none':0,
        'line_comment':0
    })
    for line in file.readlines():
        if not line.split():
            specialline['line_none'] +=1
        elif '#' in str(line):
            specialline['line_comment'] +=1
        else:
            specialline['line_value'] +=1
    return dict({'文件':filename,'特殊行':specialline})

  CmdDirPath()用于处理目录下所有文件

def CmdDirPath(command,filename,result):
    command_detail = command.replace('-s','')
    files = os.listdir(filename)
    for fi in files:
        fi_d = os.path.join(filename, fi)
        if os.path.isdir(fi_d):
            CmdDirPath(command,fi_d,result)
        else:
            result_one=CommandList(command=command_detail,filename=str(os.path.join(fi_d))),
            result.append(result_one)
    return result

  获取命令行参数

def main():
    if len(sys.argv) != 3:
        print("请输入格式为 python wc.py -l file.c 的命令")
        sys.exit(1)
    command = sys.argv[1]
    filename = sys.argv[2]
    result = CommandList(command,filename)
    print(result)
    sys.exit(1)
if __name__=="__main__":
    main()

四、运行测试:

  

 

五、小结:

  这次作业让我锻炼了许多,了解到不同的open方法以及不一样的统计单词数的方法,了解到github的用法,获益良多。

posted @ 2018-09-13 21:06  lzhoo  阅读(104)  评论(0编辑  收藏  举报