add
This commit is contained in:
+35
-8
@@ -1,11 +1,29 @@
|
||||
import time
|
||||
import traceback
|
||||
|
||||
|
||||
def get_grade(student_client, output_type="none"):
|
||||
try:
|
||||
# 获取成绩信息
|
||||
grade_data = student_client.get_grade().get("data", {})
|
||||
grade = grade_data.get("courses", [])
|
||||
# 定义重试次数上限为5次
|
||||
attempts = 5
|
||||
# 初始化grade为空列表
|
||||
grade = []
|
||||
|
||||
# 使用while循环最多重试5次获取个人信息
|
||||
while attempts > 0:
|
||||
|
||||
# 调用student_client的get_grade方法获取成绩信息
|
||||
grade_data = student_client.get_grade().get("data", {})
|
||||
grade = grade_data.get("courses", [])
|
||||
|
||||
# 如果grade不为空,跳出循环
|
||||
if grade:
|
||||
break
|
||||
|
||||
# 如果grade为空,等待1秒后重试
|
||||
time.sleep(1)
|
||||
# 减少剩余重试次数
|
||||
attempts -= 1
|
||||
|
||||
# 成绩不为空时
|
||||
if grade:
|
||||
@@ -14,13 +32,17 @@ def get_grade(student_client, output_type="none"):
|
||||
|
||||
# 遍历 grade 中的每个字典,将 title 中的中文括号替换为英文括号
|
||||
for course_data_grade in grade:
|
||||
course_data_grade["title"] = course_data_grade["title"].replace("(", "(").replace(")", ")")
|
||||
course_data_grade["title"] = (
|
||||
course_data_grade["title"].replace("(", "(").replace(")", ")")
|
||||
)
|
||||
|
||||
# 按照提交时间降序排序
|
||||
# 对于没有提交时间参数的成绩,则将提交时间设置为1970-01-01 00:00:00,否则将无法排序
|
||||
sorted_grade = sorted(
|
||||
grade,
|
||||
key=lambda x: (x["submission_time"] if x["submission_time"] else "1970-01-01 00:00:00"),
|
||||
key=lambda x: (
|
||||
x["submission_time"] if x["submission_time"] else "1970-01-01 00:00:00"
|
||||
),
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
@@ -34,16 +56,21 @@ def get_grade(student_client, output_type="none"):
|
||||
|
||||
# (百分制成绩*学分)的总和
|
||||
sum_of_percentage_grades_multiplied_by_credits = sum(
|
||||
float(course["percentage_grades"]) * float(course["credit"]) for course in filtered_grade
|
||||
float(course["percentage_grades"]) * float(course["credit"])
|
||||
for course in filtered_grade
|
||||
)
|
||||
|
||||
# GPA计算 (学分*绩点)的总和/学分总和
|
||||
gpa = "{:.2f}".format(total_xfjd / total_credit)
|
||||
|
||||
# 百分制GPA计算 (百分制成绩*学分)的总和/学分总和
|
||||
percentage_gpa = "{:.2f}".format(sum_of_percentage_grades_multiplied_by_credits / total_credit)
|
||||
percentage_gpa = "{:.2f}".format(
|
||||
sum_of_percentage_grades_multiplied_by_credits / total_credit
|
||||
)
|
||||
else:
|
||||
total_credit = total_xfjd = sum_of_percentage_grades_multiplied_by_credits = gpa = percentage_gpa = 0
|
||||
total_credit = total_xfjd = sum_of_percentage_grades_multiplied_by_credits = gpa = (
|
||||
percentage_gpa
|
||||
) = 0
|
||||
|
||||
# 初始化输出成绩信息字符串
|
||||
integrated_grade_info = "------\n成绩信息:"
|
||||
|
||||
Reference in New Issue
Block a user