2020軟工實踐第一次個人編程作業


這個作業屬於哪個課程 https://edu.cnblogs.com/campus/fzu/SE2020
這個作業要求在哪里 https://edu.cnblogs.com/campus/fzu/SE2020/homework/11167
這個作業的目標 github使用,json解析,arg解析,測試項目,性能優化分析
學號 031802513

一、PSP表格

PSP2.1 Personal Software Process Stages 預估耗時(分鍾) 實際耗時(分鍾)
Planning 計划 60 70
Estimate 估計這個任務需要多少時間 150 180
Development 開發 100 150
Analysis 需求分析 (包括學習新技術) 250 300
Design Spec 生成設計文檔 25 30
Design Review 設計復審 15 70
Coding Standard 代碼規范 (為目前的開發制定合適的規范) 10 20
Design 具體設計 50 80
Coding 具體編碼 25 70
Code Review 代碼復審 50 130
Test 測試(自我測試,修改代碼,提交修改) 30 50
Reporting 報告 25 50
Test Report 測試報告 10 20
Size Measurement 計算工作量 5 20
Postmortem & Process Improvement Plan 事后總結, 並提出過程改進計划 5 20
合計 810 1260

二、解題過程

分析需求:

python 讀取文件夾內的所有文件,解析json,統計json字段 ,python 命令行參數 , 單元測試
json解析用python自帶json庫,json.loads(json字符串),命令行參數用argparse庫,單元測試用unittest庫
json庫 https://www.runoob.com/python/python-json.html
argparse庫 https://www.cnblogs.com/cuhm/p/10643765.html
unittest庫 https://www.cnblogs.com/lsdb/p/10444943.html

三、實現過程、流程圖

四、代碼說明

命令行參數注冊


    def initArgparse(self):
        # 初始化Arg
        self.parser = argparse.ArgumentParser()
        self.parser.add_argument('-i', '--init')
        self.parser.add_argument('-u', '--user')
        self.parser.add_argument('-r', '--repo')
        self.parser.add_argument('-e', '--event')

#根據參數執行
        if self.parser.parse_args().init:

讀取目錄下所有文件

        for root, dic, files in os.walk(Address): #獲取文件夾內所有文件
            for file in files:
                if file[-5:] == '.json': #篩選后綴為.json的文件
                    findFile=True
                    json_path = file
                    filedir = open(Address+'\\'+json_path,
                             'r', encoding='utf-8')

                    while True:  #對單個文件逐行讀取
                        line = filedir.readline()
                        if line :
                            if line.strip() == '':  # 如果讀到的是空行
                                continue  # 跳過該行
                            jsondata=json.loads(line)
                            if not jsondata["type"] in ['PushEvent', 'IssueCommentEvent', 'IssuesEvent', 'PullRequestEvent']: #篩選事件
                                continue # 跳過無關事件
                            self.addEvent(jsondata)# 統計事件數量
                        else:
                            break

                    filedir.close()

分析json,統計

        if not jsondata["actor"]["login"] in self.__User.keys():
            self.__User[jsondata["actor"]["login"]] = {'PushEvent': 0, 'IssueCommentEvent': 0, 'IssuesEvent': 0,
                      'PullRequestEvent': 0}
        if not jsondata["repo"]["name"] in self.__Repo.keys():
            self.__Repo[jsondata["repo"]["name"]] = {'PushEvent': 0, 'IssueCommentEvent': 0, 'IssuesEvent': 0,
                      'PullRequestEvent': 0}

        if not jsondata["actor"]["login"] in self.__UserAndRepo.keys():
            self.__UserAndRepo[jsondata["actor"]["login"]] = {}
            self.__UserAndRepo[jsondata["actor"]["login"]][jsondata["repo"]["name"]] ={'PushEvent': 0, 'IssueCommentEvent': 0, 'IssuesEvent': 0,
                      'PullRequestEvent': 0}
        elif not jsondata["repo"]["name"] in self.__UserAndRepo[jsondata["actor"]["login"]].keys():
            self.__UserAndRepo[jsondata["actor"]["login"]][jsondata["repo"]["name"]] = {'PushEvent': 0, 'IssueCommentEvent': 0, 'IssuesEvent': 0,
                      'PullRequestEvent': 0}
        self.__User[jsondata["actor"]["login"]][jsondata['type']] += 1
        self.__Repo[jsondata["repo"]["name"]][jsondata['type']] += 1
        self.__UserAndRepo[jsondata["actor"]["login"]][jsondata["repo"]["name"]][jsondata['type']] += 1

查詢

    def getEventsByUsersAndRepos(self, username: str, reponame: str, event: str) -> int:
        #通過用戶名和倉庫名獲取事件數量
        if not self.__User.get(username,0):
            return 0
        elif not self.__UserAndRepo[username].get(reponame,0):
            return 0
        else:
            return self.__UserAndRepo[username][reponame].get(event,0)

五、單元測試


測試了預處理、讀取預處理文件以及三種查詢
覆蓋率72%,主要是命令行參數處理部分未測試到,執行時間1.217s

六、性能優化

考慮到可能具有多文件讀取,因此增加多線程功能來同時讀取多個文件。引用了python的concurrent.futures庫
參考資料https://www.cnblogs.com/zhang293/p/7954353.html

經過多次測試,發現性能並未提升,反而有所下降。經查詢,python的多線程因GIL全局解釋器鎖的原因,在計算密集型任務中效果甚微,更適合用在io密集型任務中

七. 代碼規范鏈接

https://github.com/qewpqewp/2020-personal-python/blob/master/codestyle.md

八、總結

此次作業選擇使用python的原因是對於python的代碼量不如其他兩種語言,希望能借此提高自己python水平。
收獲最大的是關於python的多線程的應用,雖然最終沒有起到作用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM