软工第一次个人编程作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/SE2020
这个作业的要求在哪里 https://edu.cnblogs.com/campus/fzu/SE2020/homework/11167
这个作业的目标 学习Git和GitHub的使用,学会基础的python语言,安装python环境
学号 031802603

一、psp表格

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

二、解题思路

拿到题目后整个人是懵逼的状态,大一大二完全没有接触过python也没有接触过实质性的开发,只完成了课内的c语言和c++的学习,在大佬的指点以及群内同学的讨论中,明确了一点方向,学习了git基础命令,查阅并且了解json库的使用。之前没有选修过python课的我留下了不学无术的泪水,感觉自己查的和示例代码写的好像不是同一个世界的东西,很痛苦的猛学。

(图为第一次把python文件添加到版本库)

三、流程图

努力理解了一些json库的应用后摸索着写了一个流程图

四、代码区

对于此时的我来说,写出一段这样成熟的代码实在是过于困难,只能够尽力阅读理解给出的示例代码
import json
import os
import argparse

class Data:
def init(self, dict_address: int = None, reload: int = 0):
if reload == 1:
self.__init(dict_address)
if dict_address is None and not os.path.exists('1.json') and not os.path.exists('2.json') and not os.path.exists('3.json'):
raise RuntimeError('error: init failed')
x = open('1.json', 'r', encoding='utf-8').read()
self.__4Events4PerP = json.loads(x)
x = open('2.json', 'r', encoding='utf-8').read()
self.__4Events4PerR = json.loads(x)
x = open('3.json', 'r', encoding='utf-8').read()
self.__4Events4PerPPerR = json.loads(x)

def __init(self, dict_address: str):
    json_list = []
    for root, dic, files in os.walk(dict_address):
        for f in files:
            if f[-5:] == '.json':
                json_path = f
                x = open(dict_address+'\\'+json_path,
                         'r', encoding='utf-8').read()
                str_list = [_x for _x in x.split('\n') if len(_x) > 0]
                for i, _str in enumerate(str_list):
                    try:
                        json_list.append(json.loads(_str))
                    except:
                        pass
    records = self.__listOfNestedDict2ListOfDict(json_list)
    self.__4Events4PerP = {}
    self.__4Events4PerR = {}
    self.__4Events4PerPPerR = {}
    for i in records:
        if not self.__4Events4PerP.get(i['actor__login'], 0):
            self.__4Events4PerP.update({i['actor__login']: {}})
            self.__4Events4PerPPerR.update({i['actor__login']: {}})
        self.__4Events4PerP[i['actor__login']][i['type']
                                     ] = self.__4Events4PerP[i['actor__login']].get(i['type'], 0)+1
        if not self.__4Events4PerR.get(i['repo__name'], 0):
            self.__4Events4PerR.update({i['repo__name']: {}})
        self.__4Events4PerR[i['repo__name']][i['type']
                                   ] = self.__4Events4PerR[i['repo__name']].get(i['type'], 0)+1
        if not self.__4Events4PerPPerR[i['actor__login']].get(i['repo__name'], 0):
            self.__4Events4PerPPerR[i['actor__login']].update({i['repo__name']: {}})
        self.__4Events4PerPPerR[i['actor__login']][i['repo__name']][i['type']
                                                      ] = self.__4Events4PerPPerR[i['actor__login']][i['repo__name']].get(i['type'], 0)+1
    with open('1.json', 'w', encoding='utf-8') as f:
        json.dump(self.__4Events4PerP,f)
    with open('2.json', 'w', encoding='utf-8') as f:
        json.dump(self.__4Events4PerR,f)
    with open('3.json', 'w', encoding='utf-8') as f:
        json.dump(self.__4Events4PerPPerR,f)

def __parseDict(self, d: dict, prefix: str):
    _d = {}
    for k in d.keys():
        if str(type(d[k]))[-6:-2] == 'dict':
            _d.update(self.__parseDict(d[k], k))
        else:
            _k = f'{prefix}__{k}' if prefix != '' else k
            _d[_k] = d[k]
    return _d

def __listOfNestedDict2ListOfDict(self, a: list):
    records = []
    for d in a:
        _d = self.__parseDict(d, '')
        records.append(_d)
    return records

def getEventsUsers(self, username: str, event: str) -> int:
    if not self.__4Events4PerP.get(username,0):
        return 0
    else:
        return self.__4Events4PerP[username].get(event,0)

def getEventsRepos(self, reponame: str, event: str) -> int:
    if not self.__4Events4PerR.get(reponame,0):
        return 0
    else:
        return self.__4Events4PerR[reponame].get(event,0)

def getEventsUsersAndRepos(self, username: str, reponame: str, event: str) -> int:
    if not self.__4Events4PerP.get(username,0):
        return 0
    elif not self.__4Events4PerPPerR[username].get(reponame,0):
        return 0
    else:
        return self.__4Events4PerPPerR[username][reponame].get(event,0)

class Run:
def init(self):
self.parser = argparse.ArgumentParser()
self.data = None
self.argInit()
print(self.analyse())

def argInit(self):
    self.parser.add_argument('-i', '--init')
    self.parser.add_argument('-u', '--user')
    self.parser.add_argument('-r', '--repo')
    self.parser.add_argument('-e', '--event')

def analyse(self):
    if self.parser.parse_args().init:
        self.data = Data(self.parser.parse_args().init, 1)
        return 0
    else:
        if self.data is None:
            self.data = Data()
        if self.parser.parse_args().event:
            if self.parser.parse_args().user:
                if self.parser.parse_args().repo:
                    res = self.data.getEventsUsersAndRepos(
                        self.parser.parse_args().user, self.parser.parse_args().repo, self.parser.parse_args().event)
                else:
                    res = self.data.getEventsUsers(
                        self.parser.parse_args().user, self.parser.parse_args().event)
            elif self.parser.parse_args().repo:
                res = self.data.getEventsRepos(
                    self.parser.parse_args().reop, self.parser.parse_args().event)
            else:
                raise RuntimeError('error: argument -l or -c are required')
        else:
            raise RuntimeError('error: argument -e is required')
    return res

if name == 'main':
a = Run()

程序运行结果如下:

五、代码规范链接

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

六、总结

大半夜才做完整篇博客,还是很清楚的认识到自己的才疏学浅,还是得认真系统的学习python语言,包括git和pycharm等工具的使用。还是太菜了都不懂怎么总结,明天开始还是得加油!!!

posted @ 2020-09-17 02:31  Eddie-c  阅读(68)  评论(2编辑  收藏