python实现什么功能_Python 实现WC功能
項目要求
基本要求
-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. 遍歷文件
defrecursive(list):
f_list=os.listdir(list)return f_list
2. 統計字符數
defstr_count(name):
with open(name,'r', encoding='UTF-8') as f:
n=0for line inf.readlines():
n+=len(line)return n
3. 統計行數
defline_count(name):
with open(name,'r', encoding='UTF-8') as f:
n=0for line inf:
n+= 1
return n
4. 統計單詞數
importredefwords_count(name):
with open(name,'r', encoding='UTF-8') as f:
n=0for line inf.readlines():
list_match= re.findall('[a-zA-Z]+', line.lower())
n+=len(list_match)return n
5. 統計空行/代碼行/注釋行數
defcode_count(name):
with open(name,'r', encoding='UTF-8') as f:
code_lines=0
comm_lines=0
space_lines=0for line inf.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:whileTrue:
line=f.readline()
comm_lines+= 1
if ("'''" in line) or ('"""' inline):break
elifline.strip():
code_lines+= 1
else:
space_lines+= 1
return code_lines, comm_lines, space_lines
6. 命令行邏輯
defwc(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实现什么功能_Python 实现WC功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android camera2 采集,视
- 下一篇: python教程苹果版_python教程