Repository navigation
Expand file tree
/
Copy pathstudents.py
More file actions
74 lines (59 loc) · 2.47 KB
/
Copy pathstudents.py
File metadata and controls
74 lines (59 loc) · 2.47 KB
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
# Student name & username list generator
import csv
import textwrap
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
type StudentRecord = tuple[str, str]
type StudentList = list[StudentRecord]
def get_student_details(csvpath: str) -> StudentList:
"""
Extracts student names & usernames from a module enrolment CSV file.
"""
details = []
with open(csvpath, "rt") as infile:
reader = csv.DictReader(infile)
for record in reader:
name = record["Student Name"].strip()
username, _ = record["Email Address"].split("@")
details.append((username, name))
return details
def write_list(data: StudentList, filename: str, numbered: bool = False) -> None:
"""
Writes student usernames & names to the given text file.
"""
with open(filename, "wt") as outfile:
for index, item in enumerate(data):
username, name = item
if numbered:
print(f"{index+1:3d} {username:8s} {name}", file=outfile)
else:
print(f"{username:8s} {name}", file=outfile)
def parse_command_line() -> Namespace:
parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
description=textwrap.dedent("""
Creates lists of student names & usernames from a module enrolment file.
The module enrolment file is the standard CSV file downloaded via
Faculty Services.
The tool outputs two lists: one sorted by student name, the other sorted
by username. List entries can optionally be numbered.
The lists can be used to map from name to username and vice versa,
reconciling the 'by username' organisation of downloaded coursework and
the 'by name' listing used by Gradebook in Minerva.
"""),
)
parser.add_argument("csv_path", help="path to module enrolment CSV file")
parser.add_argument("names_path", help="path to 'sorted by name' list")
parser.add_argument("usernames_path", help="path to 'sorted by username' list")
parser.add_argument(
"-n",
"--numbered",
action="store_true",
default=False,
help="add line numbers to both output files",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_command_line()
details = get_student_details(args.csv_path)
write_list(details, args.names_path, args.numbered)
write_list(sorted(details), args.usernames_path, args.numbered)