modify the algorithm of getGrade

This commit is contained in:
lijingwei 2020-02-10 14:04:29 +08:00
parent 09c2791134
commit 722029bdae

View File

@ -48,100 +48,117 @@ def getGrade(Ip, S):
data = json.loads(r.content.decode('utf-8'))
if data['state'] != 0:
return {'errcode': 102, 'errmsg': '教务挂了'}
#分解数据并重命名
total = data['data']['GradeStatistics']
split = data['data']['GradeList']
#成绩总览
GradeStatistics = {
'total_GPA': data['data']['GradeStatistics']['PJJD'],
'total_credit': data['data']['GradeStatistics']['SDXF'],
'total_kill': data['data']['GradeStatistics']['TGMS'],
'total_dead': data['data']['GradeStatistics']['WTGMS']
'total_GPA': total['PJJD'],
'total_credit': total['SDXF'],
'total_kill': total['TGMS'],
'total_dead': total['WTGMS']
}
#分学期成绩
#提取第一和最后一学期
first_term = split[0]['KSXNXQ']
last_term = split[len(split)-1]['KSXNXQ']
#转换成int元组
first_term = (int(first_term[0:4]),int(last_term[4:6]))
last_term = (int(last_term[0:4]),int(last_term[4:6]))
#生成中间学期
total_term = []
for i in range(last_term[0],first_term[0] + 1):
for j in range(1,3):
total_term.append(str(i) + str(j))
if i == first_term[0] and j == first_term[1]:
break
total_term.reverse()
grade_list = []
#最后一次遍历到的学期
last_term = ''
last_term_data = {}
#当前学期索引/上学期总通过/上学期总挂科/上学期总学分/上学期总学分绩点/上一课程通过次数
this_term, last_term_kill, last_term_dead, last_term_credit, last_term_c_x_g, last_lesson_kill = 0, 0, 0, 0, 0, 0
#上学期课程列表
last_term_grade_list = []
last_term_kill = 0
last_term_dead = 0
last_term_total_credit = 0
last_term_total_c_x_g = 0#sum(学分*绩点)
last_term_GPA = 0
#上一课程名称
last_lesson_name = ''
last_lesson_kill = 0
for item in data['data']['GradeList']:
#flag,将是否通过延后一个循环
flag = True
#遍历课程
for item in split:
#如果和上一个课程重名
if item['LessonInfo']['KCMC'] == last_lesson_name:
#判断是否通过
if item['YXCJ'] >= 60:
#如果通过贡献1通过
last_lesson_kill += 1
last_term_total_credit += item['XF']
grade = (item['YXCJ'] - (item['YXCJ'] % 10) - 50) // 10
grade += 0.5 if (item['YXCJ'] % 10 >= 5) else 0
last_term_total_c_x_g += item['XF'] * grade
else:
#如果学期改变
if last_term != item['KSXNXQ']:
#初始化所有变量
if last_term != '':
if last_lesson_kill > 0:
last_term_kill += 1
else:
last_term_dead += 1
last_term_GPA = last_term_total_c_x_g / last_term_total_credit
grade_list.append(
{
'term_time': last_term,
'term_GPA': last_term_GPA,
'term_kill': last_term_kill,
'term_dead': last_term_dead,
'term_credit': last_term_total_credit,
'term_grade': last_term_grade_list
}
)
last_term_grade_list = []
last_term_kill = 0
last_term_dead = 0
last_term_total_c_x_g = 0
last_term_total_credit = 0
last_term_GPA = 0
last_term = item['KSXNXQ']
#贡献总学分绩点
last_term_c_x_g += item['XF'] * (item['YXCJ'] - 50) / 10
else:#如果不重名
if flag:#将else中的判断延后一个循环
flag = False
else:
if last_lesson_name != '':
if last_lesson_kill > 0:
last_term_kill += 1
else:
last_term_dead += 1
last_lesson_kill = 0
if item['YXCJ'] >= 60:
last_lesson_kill += 1
last_term_total_credit += item['XF']
grade = (item['YXCJ'] - (item['YXCJ'] % 10) - 50) // 10
grade += 0.5 if (item['YXCJ'] % 10 >= 5) else 0
last_term_total_c_x_g += item['XF'] * grade
#如果上一课程通过
if last_lesson_kill > 0:
#贡献上学期通过数
last_term_kill += 1
else:
#贡献上学期挂科数
last_term_dead += 1
#更新上一课程名称
last_lesson_name = item['LessonInfo']['KCMC']
#新课程加入学期课程列表
last_term_grade_list.append(
{
'title': item['LessonInfo']['KCMC'],
'credit': item['XF'],
'grade': item['ShowYXCJ'],
'kill': 'yes' if (item['YXCJ'] >= 60) else 'no',
'class': item['KSXZ']
}
)
#如果不是当前学期
if item['KSXNXQ'] != total_term[this_term]:
#成绩列表添加上学期数据
grade_list.append({
'term_time': total_term[this_term],
'term_GPA': last_term_c_x_g / last_term_credit,
'term_kill': last_term_kill,
'term_dead': last_term_dead,
'term_credit': last_term_credit,
'term_grade': last_term_grade_list
})
#当前学期索引+1
this_term += 1
#初始化所有值
last_term_kill, last_term_dead, last_term_credit = 0, 0, item['XF']
last_term_grade_list = []
#如果通过
if item['YXCJ'] >= 60:
#贡献总学分绩点
last_term_c_x_g = item['XF'] * (item['YXCJ'] - 50) / 10
#贡献通过次数
last_lesson_kill += 1
else:
last_term_c_x_g = 0
else:#如果是当前学期
#贡献总学分
last_term_credit += item['XF']
#如果通过
if item['YXCJ'] >= 60:
#贡献通过数
last_lesson_kill += 1
#贡献学分绩点
last_term_c_x_g += item['XF'] * (item['YXCJ'] - 50) / 10
#加入学期成绩列表
last_term_grade_list.append({
'title': item['LessonInfo']['KCMC'],
'credit': item['XF'],
'grade': item['ShowYXCJ'],
'kill': 'yes' if (item['YXCJ'] >= 60) else 'no',
'class': item['KSXZ']
})
#补充最后一次遍历的数据
if last_lesson_kill > 0:
last_term_kill += 1
else:
last_term_dead += 1
last_term_GPA = last_term_total_c_x_g / last_term_total_credit
grade_list.append(
{
'term_time': last_term,
'term_GPA': last_term_GPA,
'term_kill': last_term_kill,
'term_dead': last_term_dead,
'term_credit': last_term_total_credit,
'term_grade': last_term_grade_list
}
)
grade_list.append({
'term_time': total_term[this_term],
'term_GPA': last_term_c_x_g / last_term_credit,
'term_kill': last_term_kill,
'term_dead': last_term_dead,
'term_credit': last_term_credit,
'term_grade': last_term_grade_list
})
#合并数据
data_cache = {
'total': GradeStatistics,
'split': grade_list
@ -218,4 +235,4 @@ def getCurrentTime(Ip, S):
)
data = json.loads(r.content.decode('utf-8'))
data_cache = data['data'].pop('DateList')
return {'errcode': 200, 'errmsg': 'ok', 'data': data_cache}
return {'errcode': 200, 'errmsg': 'ok', 'data': data_cache}