-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHUST-GPA.py
126 lines (111 loc) · 3.97 KB
/
HUST-GPA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#-*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import json
import os
import sys
# 华科算法
def HUSTGPA(grade):
if grade >= 85:
return 4.0
elif grade < 60:
return 0
else:
return 1.5 + (grade - 60) * 0.1
# 标准算法
def StandardGPA(grade):
if grade >= 90:
return 4.0
elif grade < 60:
return 0
elif grade >= 60 and grade < 70:
return 1.0
elif grade >= 70 and grade < 80:
return 2.0
elif grade >= 80 and grade < 90:
return 3.0
# 北大算法
# def PKUGPA(grade):
# if grade >= 95:
# return 4.0
# elif grade >= 90:
# return 3.7
# elif grade >= 85:
# return 3.3
# elif grade >= 80:
# return 3.0
# elif grade >= 77:
# return 2.7
# elif grade >= 73:
# return 2.3
# elif grade >= 70:
# return 2.0
# elif grade >= 67:
# return 1.7
# elif grade >= 63:
# return 1.3
# elif grade >= 60:
# return 1.0
# return 0
def PKUGPA(grade):
if grade < 60:
return 0
return 4-3 * (100 - grade) ** 2 / 1600
all_info = {'course_info': [], 'GPA': ''}
result = {'HUST': 0, 'Standard': 0, 'PKU': 0}
credit_sum = 0
def calculate(df):
global credit_sum
for i in range(int(df.columns.size / 4)):
course_name = df[df.columns[4 * i]]
course_grade = df[df.columns[4 * i + 1]]
course_credit = df[df.columns[4 * i + 2]]
is_optional = df[df.columns[4 * i + 3]]
for index, credit in enumerate(course_credit):
# 如果是一门课程 并且不是选修 且学分不为0
if not np.isnan(credit):
# 是一门课程
hust_result = ''
standard_result = ''
pku_result = ''
# 处理缓考
if course_grade[index] == "缓考/":
# 缓考的时候成绩和备注会调换位置
loc_grade = is_optional[index]
else:
loc_grade = course_grade[index]
# print(course_name[index], course_grade[index], course_credit[index], is_optional[index]);
if type(is_optional[index]) != type('') or course_grade[index] == "缓考/":
# 不是公选,或者是缓考的课程
if str.isdigit(loc_grade):
# 不考虑成绩为良等的情况
hust_result = HUSTGPA(grade=int(loc_grade))
standard_result = StandardGPA(grade=int(loc_grade))
pku_result = PKUGPA(grade=int(loc_grade))
result['HUST'] += credit * hust_result
result['Standard'] += credit * standard_result
result['PKU'] += credit * pku_result
credit_sum += course_credit[index]
all_info['course_info'].append({'course': course_name[index].strip(), 'grade': loc_grade, 'credit': course_credit[index], 'subGPA': {
"HUST": hust_result, "Standard": standard_result, "PKU": pku_result}, 'year': str(i + 1), 'is_optional': type(is_optional[index]) == type('') and is_optional[index] == "公选"})
result['HUST'] /= credit_sum
result['Standard'] /= credit_sum
result['PKU'] /= credit_sum
all_info['GPA'] = result
if __name__ == '__main__':
if (len(sys.argv) == 2):
if os.path.isfile(sys.argv[1]):
try:
calculate(pd.read_excel(sys.argv[1], header=3))
with open('result.json', 'w') as f:
f.write(json.dumps(all_info, ensure_ascii=False,
sort_keys=False, indent=4))
print(result)
except:
print("Unknown Error")
exit(0)
else:
print("Target not exist or is a dir")
exit(0)
else:
print("Please enter with correct format")