def init_assignment(self, assignment_id, student_id): super(AssignApp, self).init_assignment(assignment_id, student_id) # try to get the assignment from the database, and throw an error if it # doesn't exist gb = Gradebook(self.db_url) try: gb.find_assignment(assignment_id) except MissingEntry: if self.create_assignment: self.log.warning("Creating assignment '%s'", assignment_id) gb.add_assignment(assignment_id) else: self.fail("No assignment called '%s' exists in the database", assignment_id)
def _clean_old_notebooks(self, assignment_id, student_id): gb = Gradebook(self.db_url) assignment = gb.find_assignment(assignment_id) regexp = os.path.join( self._format_source("(?P<assignment_id>.*)", "(?P<student_id>.*)"), "(?P<notebook_id>.*).ipynb") # find a set of notebook ids for new notebooks new_notebook_ids = set([]) for notebook in self.notebooks: m = re.match(regexp, notebook) if m is None: raise RuntimeError("Could not match '%s' with regexp '%s'", notebook, regexp) gd = m.groupdict() if gd['assignment_id'] == assignment_id and gd['student_id'] == student_id: new_notebook_ids.add(gd['notebook_id']) # pull out the existing notebook ids old_notebook_ids = set(x.name for x in assignment.notebooks) # no added or removed notebooks, so nothing to do if old_notebook_ids == new_notebook_ids: return # some notebooks have been removed, but there are submissions associated # with the assignment, so we don't want to overwrite stuff if len(assignment.submissions) > 0: self.fail("Cannot modify existing assignment '%s' because there are submissions associated with it", assignment) # remove the old notebooks for notebook_id in (old_notebook_ids - new_notebook_ids): self.log.warning("Removing notebook '%s' from the gradebook", notebook_id) gb.remove_notebook(notebook_id, assignment_id)
def init_assignment(self, assignment_id, student_id): super(AssignApp, self).init_assignment(assignment_id, student_id) # try to get the assignment from the database, and throw an error if it # doesn't exist gb = Gradebook(self.db_url) try: gb.find_assignment(assignment_id) except MissingEntry: if self.create_assignment: self.log.warning("Creating assignment '%s'", assignment_id) gb.add_assignment(assignment_id) else: self.fail("No assignment called '%s' exists in the database", assignment_id) else: # check if there are any extra notebooks in the db that are no longer # part of the assignment, and if so, remove them if self.notebook_id == "*": self._clean_old_notebooks(assignment_id, student_id)
class SaveGradeCells(Preprocessor): """A preprocessor to save information about grade cells.""" db_name = Unicode("gradebook", config=True, help="Database name") db_ip = Unicode("localhost", config=True, help="IP address for the database") db_port = Integer(27017, config=True, help="Port for the database") assignment_id = Unicode(u'assignment', config=True, help="Assignment ID") def preprocess(self, nb, resources): # connect to the mongo database self.gradebook = Gradebook(self.db_name, ip=self.db_ip, port=self.db_port) self.assignment = self.gradebook.find_assignment( assignment_id=self.assignment_id) self.notebook_id = resources['unique_key'] nb, resources = super(SaveGradeCells, self).preprocess(nb, resources) return nb, resources def preprocess_cell(self, cell, resources, cell_index): if utils.is_grade(cell): grade_cell = self.gradebook.find_or_create_grade_cell( grade_id=cell.metadata.nbgrader.grade_id, notebook_id=self.notebook_id, assignment=self.assignment) grade_cell.max_score = float(cell.metadata.nbgrader['points']) # we only want the source and checksum for non-solution cells if utils.is_solution(cell): grade_cell.source = None grade_cell.checksum = None else: grade_cell.source = cell.source grade_cell.checksum = cell.metadata.nbgrader['checksum'] self.gradebook.update_grade_cell(grade_cell) self.log.debug("Recorded grade cell %s into database", grade_cell.grade_id) return cell, resources
class OverwriteGradeCells(Preprocessor): """A preprocessor to save information about grade cells.""" db_name = Unicode("gradebook", config=True, help="Database name") db_ip = Unicode("localhost", config=True, help="IP address for the database") db_port = Integer(27017, config=True, help="Port for the database") assignment_id = Unicode(u'assignment', config=True, help="Assignment ID") def preprocess(self, nb, resources): # connect to the mongo database self.gradebook = Gradebook(self.db_name, ip=self.db_ip, port=self.db_port) self.assignment = self.gradebook.find_assignment( assignment_id=self.assignment_id) self.notebook_id = resources['unique_key'] nb, resources = super(OverwriteGradeCells, self).preprocess(nb, resources) return nb, resources def preprocess_cell(self, cell, resources, cell_index): if utils.is_grade(cell): try: grade_cell = self.gradebook.find_grade_cell( grade_id=cell.metadata.nbgrader.grade_id, notebook_id=self.notebook_id, assignment=self.assignment) except: return cell, resources cell.metadata.nbgrader['points'] = grade_cell.max_score # we only want the source and checksum for non-solution cells if not utils.is_solution(cell) and grade_cell.source: old_checksum = grade_cell.checksum new_checksum = utils.compute_checksum(cell) if old_checksum != new_checksum: self.log.warning("Checksum for grade cell %s has changed!", grade_cell.grade_id) cell.source = grade_cell.source cell.metadata.nbgrader['checksum'] = grade_cell.checksum self.log.debug("Overwrote grade cell %s", grade_cell.grade_id) return cell, resources
def _clean_old_notebooks(self, assignment_id, student_id): gb = Gradebook(self.db_url) assignment = gb.find_assignment(assignment_id) regexp = os.path.join( self._format_source("(?P<assignment_id>.*)", "(?P<student_id>.*)"), "(?P<notebook_id>.*).ipynb") # find a set of notebook ids for new notebooks new_notebook_ids = set([]) for notebook in self.notebooks: m = re.match(regexp, notebook) if m is None: raise RuntimeError("Could not match '%s' with regexp '%s'", notebook, regexp) gd = m.groupdict() if gd['assignment_id'] == assignment_id and gd[ 'student_id'] == student_id: new_notebook_ids.add(gd['notebook_id']) # pull out the existing notebook ids old_notebook_ids = set(x.name for x in assignment.notebooks) # no added or removed notebooks, so nothing to do if old_notebook_ids == new_notebook_ids: return # some notebooks have been removed, but there are submissions associated # with the assignment, so we don't want to overwrite stuff if len(assignment.submissions) > 0: self.fail( "Cannot modify existing assignment '%s' because there are submissions associated with it", assignment) # remove the old notebooks for notebook_id in (old_notebook_ids - new_notebook_ids): self.log.warning("Removing notebook '%s' from the gradebook", notebook_id) gb.remove_notebook(notebook_id, assignment_id)
class SaveAutoGrades(Preprocessor): """Preprocessor for saving out the autograder grades into a MongoDB""" db_name = Unicode("gradebook", config=True, help="Database name") db_ip = Unicode("localhost", config=True, help="IP address for the database") db_port = Integer(27017, config=True, help="Port for the database") assignment_id = Unicode(u'assignment', config=True, help="Assignment ID") def preprocess(self, nb, resources): # connect to the mongo database self.gradebook = Gradebook(self.db_name, ip=self.db_ip, port=self.db_port) self.student = self.gradebook.find_student( student_id=resources['nbgrader']['student_id']) self.assignment = self.gradebook.find_assignment( assignment_id=self.assignment_id) self.notebook = self.gradebook.find_or_create_notebook( notebook_id=resources['unique_key'], student=self.student, assignment=self.assignment) # keep track of the number of comments we add self.comment_index = 0 # process the cells nb, resources = super(SaveAutoGrades, self).preprocess(nb, resources) return nb, resources def _add_comment(self, cell, resources): """Graders can optionally add comments to the student's solutions, so add the comment information into the database if it doesn't already exist. It should NOT overwrite existing comments that might have been added by a grader already. """ # retrieve or create the comment object from the database comment = self.gradebook.find_or_create_comment( notebook=self.notebook, comment_id=self.comment_index) # update the number of comments we have inserted self.comment_index += 1 self.log.debug(comment) def _add_score(self, cell, resources): """Graders can override the autograder grades, and may need to manually grade written solutions anyway. This function adds score information to the database if it doesn't exist. It does NOT override the 'score' field, as this is the manual score that might have been provided by a grader. """ # these are the fields by which we will identify the score # information grade = self.gradebook.find_or_create_grade( notebook=self.notebook, grade_id=cell.metadata['nbgrader']['grade_id']) # deterine what the grade is grade.autoscore, grade.max_score = utils.determine_grade(cell) # Update the grade information and print it out self.gradebook.update_grade(grade) self.log.debug(grade) def preprocess_cell(self, cell, resources, cell_index): # if it's a solution cell, then add a comment if utils.is_solution(cell): self._add_comment(cell, resources) # if it's a grade cell, the add a grade if utils.is_grade(cell): self._add_score(cell, resources) return cell, resources
class SaveAutoGrades(Preprocessor): """Preprocessor for saving out the autograder grades into a MongoDB""" db_name = Unicode("gradebook", config=True, help="Database name") db_ip = Unicode("localhost", config=True, help="IP address for the database") db_port = Integer(27017, config=True, help="Port for the database") assignment_id = Unicode(u'assignment', config=True, help="Assignment ID") def preprocess(self, nb, resources): # connect to the mongo database self.gradebook = Gradebook(self.db_name, ip=self.db_ip, port=self.db_port) self.student = self.gradebook.find_student( student_id=resources['nbgrader']['student_id']) self.assignment = self.gradebook.find_assignment( assignment_id=self.assignment_id) self.notebook = self.gradebook.find_or_create_notebook( notebook_id=resources['unique_key'], student=self.student, assignment=self.assignment) # keep track of the number of comments we add self.comment_index = 0 # process the cells nb, resources = super(SaveAutoGrades, self).preprocess(nb, resources) return nb, resources def _add_comment(self, cell, resources): """Graders can optionally add comments to the student's solutions, so add the comment information into the database if it doesn't already exist. It should NOT overwrite existing comments that might have been added by a grader already. """ # retrieve or create the comment object from the database comment = self.gradebook.find_or_create_comment( notebook=self.notebook, comment_id=self.comment_index) # update the number of comments we have inserted self.comment_index += 1 self.log.debug(comment) def _add_score(self, cell, resources): """Graders can override the autograder grades, and may need to manually grade written solutions anyway. This function adds score information to the database if it doesn't exist. It does NOT override the 'score' field, as this is the manual score that might have been provided by a grader. """ # these are the fields by which we will identify the score # information grade = self.gradebook.find_or_create_grade( notebook=self.notebook, grade_id=cell.metadata['nbgrader']['grade_id']) # set the maximum earnable score points = float(cell.metadata['nbgrader']['points']) grade.max_score = points # If it's a code cell and it threw an error, then they get # zero points, otherwise they get max_score points. If it's a # text cell, we can't autograde it. if cell.cell_type == 'code': grade.autoscore = points for output in cell.outputs: if output.output_type == 'pyerr': grade.autoscore = 0 break else: grade.autoscore = None # Update the grade information and print it out self.gradebook.update_grade(grade) self.log.debug(grade) def preprocess_cell(self, cell, resources, cell_index): # if it's a solution cell, then add a comment if utils.is_solution(cell): self._add_comment(cell, resources) # if it's a grade cell, the add a grade if utils.is_grade(cell): self._add_score(cell, resources) return cell, resources