福州大学软件工程实践个人编程作业

这个作业属于哪个课程 软件工程2020秋学期
这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/SE2020/homework/11167
这个作业的目标 制作一个程序统计和分析 GitHub 的用户行为数据。
学号 031802409

一.PSP表格

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

二.解题思路

思路分析

拿到题目的时候完全不知道要做什么,查了两天的资料才勉强开始理解题目(用python)
可以用以下两方面来说

git部分 代码部分

git的操作主要是通过这个网站:

https://www.cnblogs.com/jhy16193335/p/GitHub.html

代码部分为以下4点

1.数据的解析
2.json文件的读入
3.多线程操作的学习
4.数据统计

三.学习设计过程

学习进度条:

1.python读json文件:


读取文件夹下的所有文件

2.if _ _ name _ _ == '_ _ main _ _'

如果本模块是被直接运行的,则代码块被运行,如果本模块是被导入到其它模块中去,则处于 name 中的代码不被运行。

3.github的使用

在经历了一脸懵逼的阅读题目天书后,查找了一定量的资料,和接受了大佬的教育后
下载了github deskhop

4.Python中__init__的用法和理解

两个下划线开头的函数是声明该属性为私有,不能在类的外部被使用或访问。而__init__函数(方法)支持带参数类的初始化,也可为声明该类的属性(类中的变量)。__init__函数(方法)的第一个参数必须为self,后续参数为自己定义。

5.raise的用法

6.单元测试的学习

网站:https://search.bilibili.com/all?keyword=python单元测试&from_source=nav_recommend_new
以及CSDN

流程图

四.代码

函数

带参数类的初始化

    def __init__(self, isFirst:int =0, file_ad:int=None):
        if isFirst == 1:
            if(file_ad == None):
                raise RuntimeError('error: init failed')#主动显示错误
            self.FileJAnalyse(file_ad)
            self.SaveToLocal()
        else:
            self.localU={}
            self.localR={}
            self.localUR={}
            if self.ReadLocal() == False:
                raise RuntimeError("file is not exist")
        self.uEvent = {}
        self.rEvent = {}
        self.urEvent = {}

json解析

    def FileJAnalyse(self,file_ad:str):#解析文件夹中的所有json
        for root, dic, files in os.walk(file_ad):
            for f in files:
                if f[-5:] == ".json":
                    jsonPath = f
                    self.JsonAnalyse(file_ad, jsonPath)

    def JsonAnalyse(self, file_ad:str, jPath:str):#单个json解析函数
        f = open(file_ad + '\\' + jPath, 'r', encoding='utf-8')
        try:
            while True:
                stmp = f.readline()
                if stmp:
                    dtmp = json.loads(stmp)
                    if not dtmp["type"] in ['PushEvent','IssueCommentEvent','IssuesEvent','PullRequestEvent']:
                        continue
                    if not dtmp["actor"]["login"] in self.uEvent.keys():
                        event = {'PushEvent':0,'IssueCommentEvent':0,'IssuesEvent':0,'PullRequestEvent':0}
                        self.uEvent[dtmp["actor"]["login"]] = event
                    if not dtmp["repo"]["name"] in self.rEvent.keys():
                        event = {'PushEvent':0,'IssueCommentEvent':0,'IssuesEvent':0,'PullRequestEvent':0}
                        self.rEvent[dtmp["repo"]["name"]] = event
                    if not dtmp["actor"]["login"]+'&'+dtmp["repo"]["name"] in self.urEvent.keys():
                        event = {'PushEvent': 0, 'IssueCommentEvent': 0,'IssuesEvent': 0, 'PullRequestEvent': 0}
                        self.urEvent[dtmp["actor"]["login"] + '&' + dtmp["repo"]["name"]] = event

                    self.uEvent[dtmp["actor"]["login"]][dtmp['type']] += 1
                    self.rEvent[dtmp["repo"]["name"]][dtmp['type']] += 1
                    self.urEvent[dtmp["actor"]["login"] + '&' + dtmp["repo"]["name"]][dtmp['type']] += 1
                else:
                    break
        except:
            pass
        finally:
            f.close()

对json内容的位置进行判断以及读取

  def ReadLocal(self) -> bool:#判断数据是否成功存在本地,若不存在则报错,若存在则读取
        if not os.path.exists('user.json') and not os.path.exists(
                'repo.json') and not os.path.exists('userrepo.json'):
            return False
        x = open('user.json', 'r', encoding='utf-8').read()
        self.localU = json.loads(x)
        x = open('repo.json', 'r', encoding='utf-8').read()
        self.localR = json.loads(x)
        x = open('userepo.json', 'r', encoding='utf-8').read()
        self.localUR = json.loads(x)
        return True

    def ReadTheLocal(self):#读取json中的内容
        try:
            with open('user.json', 'w', encoding = 'utf-8') as f:
                json.dump(self.uEvent, f)
            with open('repo.json', 'w', encoding = 'utf-8') as f:
                json.dump(self.rEvent, f)
            with open('userepo.json', 'w', encoding = 'utf-8') as f:
                json.dump(self.urEvent, f)
        except:
            raise RuntimeError("save error")
        finally:
            f.close()

五.单元测试截图和描述

单元测试

import Data
import unittest

class TestClass(unittest.TestCase):
    def test_init(self):
        data = Data(1,"json")
        res = data.ReadLocal()
        assert res == True
    def test_data_queryu(self):
        data = Data()
        res = Data.QueryByUser(data, 'jiang', 'PushEvent' )
        assert res == 1
    def test_data_queryr(self):
        data = Data()
        res = Data.QueryByRepo(data, 'x', 'PushEvent')
        assert res == 1
    def test_data_queryur(self):
        data = Data( )
        res = data.QueryByUserAndRepo( 'jiang', 'x', ' PushEvent')
        assert res == 1

if __name__ == '__main__':
    unittest.main()

这里出问题了还没解决。。。

性能测试

六.代码规范

https://github.com/jql0108/2020-personal-python/blob/master/requirements.txt

七.总结

对我而言是相当曲折和艰难的一次作业,很多知识都还没理解就直接应用,而且还剽窃了许多示例代码,理解困难
感觉还是学到了挺多东西的,最近一段时间都会继续学习

posted @ 2020-09-16 21:32  江小皮  阅读(255)  评论(2编辑  收藏  举报