1. 程式人生 > >Python 實現WC功能

Python 實現WC功能

get comm 獲取文件 rep nes width target lin devel

GitHub倉庫:https://github.com/15crmor/PAC

項目要求

  • 基本要求

    • -c 統計文件字符數 (實現)

    • -w 統計文件詞數 (實現)

    • -l 統計文件行數(實現)

  • 擴展功能

    • -s 遞歸處理目錄下符合條件得文件(實現)
    • -a 返回文件代碼行 / 空行 / 註釋行(實現)
  • 高級功能

    • -x 圖形化界面(未實現)

解題思路

  •  實現對文本的統計
    • 讀取文件
    • 使用正則表達式處理文本內容
  • 再實現拓展功能更復雜的統計及批量操作
    • 用os模塊獲取文件以及判斷是文件或目錄
    • 遍歷目錄下所有符合的文件
  • 最後實現命令參數解析
    • 用sys模塊實現在命令行解析參數

設計

  • 將各個功能放在不同文件中
    •  主文件及相應模塊
      •  WC.py:
        • recursive(list) (遍歷文件)
        • wc(f, arg) (實現命令參數解析)
    •  統計字符文件及模塊
      •  strCount.py:
        • str_count(name)
    •  統計行數文件及模塊:
      •  lineCount.py:
        • line_count(name)
    • 統計單詞文件及模塊:
      •  wordsCount.py:
        • words_count(name)
    • 統計代碼行/空行/註釋行文件及模塊:
      • codeCount.py:
        • code_count(name)

流程圖

技術分享圖片

代碼說明

1. 遍歷文件

def recursive(list):
    f_list = os.listdir(list)
    return f_list

2. 統計字符數

def str_count(name):
    with open(name, r, encoding=UTF-8) as f:
        n = 0
        for line in f.readlines():
            n += len(line)
    return n

3. 統計行數

def line_count(name):
    with open(name, 
r, encoding=UTF-8) as f: n = 0 for line in f: n += 1 return n

4. 統計單詞數

import re

def words_count(name):
    with open(name, r, encoding=UTF-8) as f:
        n = 0
        for line in f.readlines():
            list_match = re.findall([a-zA-Z]+, line.lower())
            n += len(list_match)
    return n

5. 統計空行/代碼行/註釋行數

def code_count(name):
    with open(name, r, encoding=UTF-8) as f:
        code_lines = 0
        comm_lines = 0
        space_lines = 0
        for line in f.readlines():
            if line.strip().startswith(#):
                comm_lines += 1
            elif line.strip().startswith("‘‘‘") or line.strip().startswith("""):
                comm_lines += 1
            elif line.count(""") == 1 or line.count("‘‘‘") == 1:
                while True:
                    line = f.readline()
                    comm_lines += 1
                    if ("‘‘‘" in line) or (""" in line):
                        break
            elif line.strip():
                code_lines += 1
            else:
                space_lines += 1

    return code_lines, comm_lines, space_lines

6. 命令行邏輯

def wc(f, arg):
    if arg[1] == -c:
        str_num = str_count(f)
        print(f + "文件字符數為: ", str_num)
    elif arg[1] == -w:
        word_num = words_count(f)
        print(f + "文件單詞數為:", word_num)
    elif arg[1] == -l:
        line_num = line_count(f)
        print(f + "文件行數為:", line_num)
    elif arg[1] == -a:
        code_lines_num, comm_lines_num, space_lines_num = code_count(f)
        print(f + "文件代碼行為:", code_lines_num)
        print("註釋行為:", comm_lines_num)
        print("空行為:", space_lines_num)

測試運行

由於事先設置了工作路徑所以默認路徑與代碼所在路徑不同

  • 基本模塊測試

技術分享圖片

  • 擴展模塊測試

技術分享圖片

  • 遞歸遍歷文件夾下文件測試

技術分享圖片

  • 文件名出錯時

技術分享圖片

代碼覆蓋率

技術分享圖片

PSP

PSP2.1

Personal Software Process Stages

預估耗時(分鐘)

實際耗時(分鐘)

Planning

計劃

60

60

· Estimate

· 估計這個任務需要多少時間

60

60

Development

開發

300

360

· Analysis

· 需求分析 (包括學習新技術)

60

100

· Design Spec

· 生成設計文檔

30

30

· Design Review

· 設計復審 (和同事審核設計文檔)

20

30

· Coding Standard

· 代碼規範 (為目前的開發制定合適的規範)

30

20

· Design

· 具體設計

30

30

· Coding

· 具體編碼

240

300

· Code Review

· 代碼復審

30

40

· Test

· 測試(自我測試,修改代碼,提交修改)

60

60

Reporting

報告

20

30

· Test Report

· 測試報告

60

60

· Size Measurement

· 計算工作量

20

20

· Postmortem & Process Improvement Plan

· 事後總結, 並提出過程改進計劃

20

30

合計

1040

1220

項目總結

  以前寫代碼從沒考慮過這麽多,總是思考一陣之後便直接上手,遇到什麽問題就查書查網上資料解決,突然想改就把之前寫的模塊推翻重來,因此也做了不少無用功,而且也很少總結,現在這樣雖然工作量多了但是卻感覺比以前開發要更快,少走了不少彎路,而且有寫了博客後也感覺比之前掌握的更加紮實。

Python 實現WC功能