Last active
September 13, 2016 04:51
-
-
Save randy3k/6922c194eee4a4f8dbfbad9a20bec4b7 to your computer and use it in GitHub Desktop.
revising released notebooks which are downloaded by students
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 nbformat | |
import os | |
release_dir = "/home/randy3k/nbgrader/sts437/release" | |
assignment = "sts437-hw1" | |
assignment_dir = os.path.join(release_dir, assignment) | |
problems = [f for f in os.listdir(assignment_dir) if os.path.splitext(f)[1] == ".ipynb"] | |
users = [d for d in os.listdir("/home") if os.path.isdir("/home/{}".format(d))] | |
def solution_cells(nb): | |
ret = {} | |
for cell in nb.cells: | |
if "nbgrader" in cell.metadata: | |
if cell.metadata.nbgrader.solution: | |
grade_id = cell.metadata.nbgrader.grade_id | |
ret.update({grade_id: cell.source}) | |
return ret | |
for u in users: | |
for p in problems: | |
try: | |
target = "/home/{}/{}/{}".format(u, assignment, p) | |
nb_user = nbformat.read(target, 4) | |
source = os.path.join(release_dir, "{}/{}".format(assignment, p)) | |
nb_skeleton = nbformat.read(source, 4) | |
solutions = solution_cells(nb_user) | |
for cell in nb_skeleton.cells: | |
if "nbgrader" in cell.metadata: | |
if cell.metadata.nbgrader.solution: | |
grade_id = cell.metadata.nbgrader.grade_id | |
if grade_id in solutions: | |
cell.source = solutions[grade_id] | |
nbformat.write(nb_skeleton, target, 4) | |
print(target) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment