WC program in Python

Github源程序代码链接

项目相关要求

  • 基本功能
    • 统计C语言源文件的字符数(完成)
    • 统计C语言源文件的词的数目(完成)
    • 统计C语言源文件的行数(完成)
  • 拓展功能
    • 递归处理目录下符合条件的文件(待完成)
    • 返回更复杂的数据(代码行 / 空行 / 注释行)(完成)
  • 高级功能
    • 实现图形界面(待完成)

## 题目解析

  • 基本功能解析
    • 字符定义:可打印字符,即不包括\r、\n等不可打印字符
    • 词定义:单词和数字
    • 行定义:由换行符分割的字符串
  • 扩展功能解析:
    • 空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符
    • 代码行:本行包括多于一个字符的代码
    • 注释行:本行不是代码行,并且本行包括注释

解题心理路程

  • 首先是理解题目,了解题目中需要完成的功能模块是什么

  • 接下来则是考虑每个功能模块应该怎么进行实现

    • 需要涉及什么技术

      首先是文件的读取

      其次是对读取进来的文件进行处理 -- 正则表达式

    • 用什么程序语言进行实现

      使用c、Java、Python都能够实现相应的功能

      但是由于语言的特性和熟悉程度不同 -- 实现代价不同

      由于本人对Python比较熟悉且Python处理文件超级方便的 -- 所以敲定Python

功能模块设计

基本功能模块(每个模块由一个函数实现,提高模块间的独立性)

  • 读取文件功能模模块 read_file(file_path)

  • 统计字符数功能模块 char_counter(lines)

  • 统计单词数功能模块 word_counter(lines)

  • 统计行数功能模块 line_counter(lines)

扩展功能模块

  • 实现空行、代码行和注释行的统计 extended_function_a(lines)

程序运行流程及模块间的关系

  • 程序运行时,首先会使用read_file(file_path)函数读取相应文件的内容
  • 然后根据传递的参数判断需要执行的功能
    • WC.exe -c filename 统计字符数
    • WC.exe -w filename 统计单词数
    • WC.exe -l filename 统计行数
    • WC.exe -a filename 统计空行/代码行/注释行数
  • 最后将结束打印输出

程序流程图

程序代码说明

(由于不想在源程序中引入中文,混淆词的定义,所以注释使用了英文)

1.读取文件内容,并转化为list

"""
read the file contents
parameters
        file_path(string):the file path
return:
        lines(list):the file content
"""
def read_file(file_path):
    with open(file_path, encoding='UTF-8') as file:
        lines = file.readlines()
    return lines

2.统计字符数

"""
count the char num of the file
parameters:
        lines(list):the file content
return:
        char_num(int):the char num of the file
"""
def char_counter(lines):
    char_num = 0
    for line in lines:
        line = re.sub('[^\w]', '', line)
        char_num += len(line)
    return char_num

3.统计单词数

 """
count the word num of the file
parameters:
        lines(list):the file content
return:
        word_num(int):the word num of the file
"""
def word_counter(lines):
    word_num = 0
    for line in lines:
        line = re.split('[^\w]+', line)
        while "" in line:
            line.remove("")
        word_num += len(line)
    return word_num

4.统计行数

"""
count the line num of the file
parameters:
        lines(list):the file content
return:
        line_num(int):the line num of the file
"""
def line_counter(lines):
    line_num = len(lines)
    return line_num

5.统计空行/代码行/注释行数量

"""
count the null/coding/annotation line num of the file
parameters:
        lines(list):the file content
return:
        null_line_num(int):the null line num of the file
        coding_line_num(int):the coding line num of the file
        annotation_line_num(int):the annotation line num of the file
"""
def extended_function_a(lines):
    null_line_num = 0
    coding_line_num = 0
    annotation_line_num = 0
    is_annotation = False
    for line in lines:
        is_null = False

        # the null line test
        null_line = re.sub('\s', '', line)
        if len(null_line) <= 1:
            null_line_num += 1
            is_null = True

        # the annotation line test
        # the head of annotation with """
        if '"""' in line and len(line.split('"""')[0].strip()) == 0 and is_annotation == False:
            annotation_line_num += 1
            is_annotation = True
            continue
        # the tail of annotation with """
        elif '"""' in line and len(line.split('"""')[-1].strip()) == 0 and is_annotation == True:
            annotation_line_num += 1
            is_annotation = False
            continue
        # the annotation content with """
        elif is_annotation == True:
            annotation_line_num += 1
            continue

        # the annotation line with #
        if '#' in line:
            first_part = re.sub('\s', '', re.split('#', line)[0])
            if len(first_part) <= 1:
                annotation_line_num += 1
                continue

        if is_null == True:
            continue

        # the coding line test
        coding_line_num += 1

    return null_line_num, coding_line_num, annotation_line_num

测试运行

这里对项目的源程序文件,即对WC.py源文件进行了测试

  • 在运行程序时,会有程序相应的使用方法提示

  • 在输入一些错误的参数或者命令时,也会有相应的提示

  • 传递的文件路径出错提示

  • 基本功能模块测试

  • 扩张功能模块测试 -- 空行/代码行/注释行统计

程序覆盖率

PSP表格

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 20 20
Estimate 估计这个任务需要多少时间 20 20
Development 开发 180 240
Analysis 需求分析(包括学习新技术) 20 30
Design Spec 生成设计文档 20 30
Design Review 设计复审 15 15
Coding Standard 代码规范 (为目前的开发制定合适的规范) 15 15
Design 具体设计 20 20
Coding 具体编码 180 240
Code Review 代码复审 20 30
Test 测试(自我测试,修改代码,提交修改) 60 60
Reporting 报告 120 150
Test Report 测试报告 30 40
Size Measurement 计算工作量 20 20
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 20 20
合计 800 960

项目小结

​ 以前一拿到项目,在思考完如何进行实现之后,都是直接开始编程实现,从来不会对项目的每个环节所花费的时间进行估计,自然也就不知道时间浪费在那些地方了,这次通过编写PSP表格,则可以清晰地看到时间的分配情况。另外,以前也没有写博客总结的习惯,东西写完了就放在一边晾着,时间久了就忘记了,在写博客的过程中,需要进行回顾和总结,对知识的巩固很有帮助。

猜你喜欢

转载自www.cnblogs.com/zzlian/p/9647171.html
wc