-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate_grades.py
executable file
·75 lines (58 loc) · 2.58 KB
/
calculate_grades.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
#!/usr/bin/env python3
import csv
import sys
import config
def load_teams_info():
if not config.TEAMS_INFO_PATH.is_file():
sys.exit('Error: teams info file does not exist')
with open(config.TEAMS_INFO_PATH, newline='') as csv_file:
reader = csv.DictReader(csv_file, delimiter='\t')
return list(reader)
def main():
if not config.GRADING_OUTPUT_FOLDER.is_dir():
sys.exit('Error: grading folder does not exist')
teams_info = load_teams_info()
config.GRADES_OUTPUT_FOLDER.mkdir(exist_ok=True)
points_to_remove_files = (file for file in config.GRADING_OUTPUT_FOLDER.rglob(config.POINTS_TO_REMOVE_FILENAME) if file.is_file())
team_files = {file.parent.name: file for file in points_to_remove_files}
# Calculate team grade totals
team_fieldnames = ['Team', 'Grade', 'Feedback']
team_grades = {}
for team_name, filename in team_files.items():
with open(filename) as file:
grade = config.POINTS_TOTAL
lines = file.readlines()
for line in lines:
points_to_remove = float(line.split(':')[0])
grade += points_to_remove
team_grades[team_name] = {
team_fieldnames[0]: team_name,
team_fieldnames[1]: grade,
team_fieldnames[2]: ''.join(lines)
}
# Generate individual grades from team info
individual_fieldnames = ['ID', 'FirstName', 'LastName', 'Email', 'Team', 'Grade', 'Feedback']
individual_grades = []
for team_info in teams_info:
grade_info = team_info.copy()
if team_info['Team'] in team_grades:
grade_info.update(team_grades[team_info['Team']])
else:
grade_info['Grade'] = 0
grade_info['Feedback'] = f'-{config.POINTS_TOTAL}: Aucun travail remis\n'
individual_grades.append(grade_info)
# Write team grades to csv
with open(config.TEAM_GRADES_PATH, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=team_fieldnames)
writer.writeheader()
for row in sorted(team_grades.values(), key=lambda x: x[team_fieldnames[0]]):
writer.writerow(row)
# Write individual grades to csv
with open(config.INDIVIDUAL_GRADES_PATH, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=individual_fieldnames)
writer.writeheader()
for row in sorted(individual_grades, key=lambda x: x[individual_fieldnames[0]]):
writer.writerow(row)
if __name__ == '__main__':
main()
# Made by Misha Krieger-Raynauld and Simon Gauvin