-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_grade
executable file
·45 lines (33 loc) · 1.56 KB
/
print_grade
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
#!/usr/bin/env python3
# This script reads the environment variable GRADE_TABLE and prints the results it contains in human-readable form
import sys, os, json
from grade_utils import round_if_integral, color_print, error_message, print_result
def print_grades():
"""Print the contents of GRADE_TABLE in human-readable form, returning True on success, False on failure"""
# Read in GRADE_TABLE
grade_data = os.getenv('GRADE_TABLE')
if grade_data is None:
error_message("GRADE_TABLE is empty")
return False
# Convert the GRADE_TABLE from JSON to a dictionary
try:
grades = json.loads(grade_data)
except json.JSONDecodeError:
error_message("GRADE_TABLE is invalid")
return False
# Print results
if not os.getenv("GRADE_QUIET"):
color_print("blue", "RESULTS:")
color_print("blue", "---------------------")
for grade_type in ("success", "failure", "extra", "missed"):
for points, description in grades[grade_type]:
print_result(grade_type, points, description)
# Print total score
points_possible = round_if_integral(sum(points for points, description in grades["success"] + grades["failure"]))
points_earned = round_if_integral(sum(points for points, description in grades["success"] + grades["extra"]))
if not os.getenv("GRADE_QUIET"):
color_print("blue", "---------------------")
color_print("blue", f"TOTAL: {points_earned}/{points_possible}")
return True
if __name__ == "__main__":
if not print_grades(): sys.exit(1)