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
|
#!/usr/bin/env python3
"""Evaluate results from a moodle quiz report HTML"""
import argparse
import csv
import locale
from bs4 import BeautifulSoup
def print_group_means(grades: dict[str, int], groups_csv_path: str):
"""Print the means of groups"""
with open(groups_csv_path, 'r', encoding='utf-8') as groups_csv_file:
lines = groups_csv_file.readlines()
groups_data = [l.strip().split(',') for l in lines]
groups = {}
for student, group in groups_data:
groups[student] = group
results: dict[str, list[int]] = {}
for student, grade in grades.items():
group = groups[student]
results[group] = [grade] + results.get(group, [])
for group, group_results in results.items():
mean = sum(group_results) / len(group_results)
print(f'{group}: {mean}')
def main():
"""Entry point loading grades and evaluating them"""
parser = argparse.ArgumentParser()
parser.add_argument("--points",
help="The maximum amount of points for the grades",
type=int,
default=3)
parser.add_argument("--scale",
help="The scale of the grades",
default="0-100")
parser.add_argument(
"--groups-csv",
help="CSV file containing student names and their groups")
parser.add_argument("report", help="The HTML file containing the report")
args = parser.parse_args()
scale = args.scale.split('-')
scale = (int(scale[0]), int(scale[1]))
with open(args.report, 'r', encoding='utf-8') as report_file:
report = BeautifulSoup(report_file.read(), 'html.parser')
grades = {}
# Collect all results
for card in report.find_all(class_="setReport-studentCard"):
studentInitials = card.find(class_="setReport-studentCard-name").string
grade = card.find(class_="scorePill").contents[0].contents[0]
if grade != 'Abs.':
grades[studentInitials] = int(grade)
if args.groups_csv:
print_group_means(grades, args.groups_csv)
else:
results = grades.values()
mean = sum(results) / len(results)
print(f'mean: {mean} -> {args.points*(mean-scale[0])/scale[1]}')
if __name__ == '__main__':
main()
|