Created
September 11, 2023 19:17
-
-
Save robobenklein/f0d423830837a1826903fd255a2e26a4 to your computer and use it in GitHub Desktop.
Canvas grades file Python context object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from pathlib import Path | |
class CanvasGradeFile(): | |
def __init__(self, file: Path): | |
self.file = file | |
def __enter__(self): | |
with self.file.open('r') as f: | |
self.reader = csv.DictReader(f) | |
self.rows = list([r for r in self.reader]) | |
self.fieldnames = self.reader.fieldnames | |
print(f"Gradebook loading assignments ...") | |
self.assignments = {} | |
self.maxpoints = {} | |
for i, (assignment, points) in enumerate(self.rows[0].items()): | |
print(f"{i}: {points} points, \"{assignment}\"") | |
self.assignments[assignment] = i | |
self.maxpoints[assignment] = points | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
with self.file.open('w') as f: | |
writer = csv.DictWriter(f, self.fieldnames) | |
writer.writeheader() | |
for r in self.rows: | |
writer.writerow(r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example usage: | |
assignment = "Practice Assignment (12345678)" | |
with CanvasGradeFile(Path(args.gradefile)) as gradebook: | |
for i, graderow in enumerate(gradebook.rows): | |
netid = graderow[canvas_netid_col] | |
# determine a grade or something | |
graderow[assignment] = 10 | |
# now you can upload the modified CSV right back into canvas to update the grades |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment