Skip to content

Instantly share code, notes, and snippets.

@robobenklein
Created September 11, 2023 19:17
Show Gist options
  • Save robobenklein/f0d423830837a1826903fd255a2e26a4 to your computer and use it in GitHub Desktop.
Save robobenklein/f0d423830837a1826903fd255a2e26a4 to your computer and use it in GitHub Desktop.
Canvas grades file Python context object
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)
# 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