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模塊運用的不熟練和代碼本身的邏輯性有一些問題,但是經過了一些努力后,也算是勉強解決了這些問題。而在其中,我發現了我對遞歸函數的使用的不熟練和對條件測試的不理解的這些缺點,這些也成為了我這次作業極大的絆腳石。我希望能夠在接下來的時間里,努力提高自己,以完成這些作業的目的。
