def renamefile(oldname, newname, MAKE_CHANGES=False, force=False): """Function to rename a file using git and updates all links to the file and checks. Parameters ---------- oldname : string Current name of the file newname : string New name for file MAKE_CHANGES : boolean Dry run name change to see what files are affected. force : boolean Ignore warnings and force the copy """ old_nbfile = nbfilename(oldname) if not oldname == str(old_nbfile): print( f"WARNING: old file {oldname} does not conform to naming standard") if not force: print(f" Set force=True to change anyway") return oldstudentversion = f"{oldname[:-17]}" else: old_nbfile.isInstructor = False oldstudentversion = str(old_nbfile) new_nbfile = nbfilename(newname) if not newname == str(new_nbfile): print(f"ERROR: new file {newname} does not conform to naming standard") print(f" using {str(new_nbfile)}") #STEP 1. Move instructor file to new name cmd = f"git mv {oldname} {str(new_nbfile)} " if MAKE_CHANGES: os.system(cmd) else: print(f"TEST: {cmd}") #Forcing new file to conform to the file standard new_nbfile.isInstructor = False newstudentversion = str(new_nbfile) print(f" Replaceing {oldstudentversion} with {newstudentversion}") directory = Path('.') for file in directory.glob('*.ipynb'): temp_np_file = nbfilename(str(file)) if temp_np_file.isInstructor: with open(file, encoding="utf-8") as f: s = f.read() if oldstudentversion in s: s = s.replace(oldstudentversion, newstudentversion) if MAKE_CHANGES: print("writing changed file") with open(file, "w", encoding="utf-8") as f: f.write(s) else: print(f"TEST: Student File Reference in {file}")
def changeprefix(filename, datestr, MAKE_CHANGES=False, force=False): """Migrate a notebook from the filename to the new four digit date string Parameters ---------- filename : string Current name of the Instructor notebook with the date prefix datestring : string New Datestring of the form MMDD (MONTH, DAY) MAKE_CHANGES : boolean Dry run name change to see what files are affected. force : boolean Ignore warnings and force the copy """ nbfile = nbfilename(filename) if not nbfile.isDate: print("ERROR: file not formated as a date file") return directory = Path('.') files = directory.glob('*.ipynb') if not Path(filename).exists(): print(f"ERROR: File {filename} not found in directory") return oldname = filename newname = f"{datestr}{oldname[4:]}" renamefile(oldname, newname, MAKE_CHANGES, force)
def __init__(self, filename, grading_folder='./AutoGrader'): nbfile = nbfilename(filename) if nbfile.isInstructor: raise Exception( "Instructor file error: Input student version filename not the instructor version." ) corefile = Path(filename) if not corefile.exists(): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), _notebook) self.core_assignment_name = corefile.stem self.grading_folder = Path(grading_folder) self.source_folder = Path(self.grading_folder, 'source', self.core_assignment_name) self.source_file = Path(self.source_folder, f'{self.core_assignment_name}-STUDENT.ipynb') self.release_folder = Path(self.grading_folder, 'release', self.core_assignment_name) self.release_file = Path(self.release_folder, f'{self.core_assignment_name}-STUDENT.ipynb') #Give OS time to make folders (Helps bugs on some systems) time.sleep(2)
def makestudent(self, tags=None, studentfolder=''): """Make a Student Version of the notebook""" instructor_fn = self.filename instructorfile = nbfilename(instructor_fn) # TODO: check all links in the directory for name change. if not str(instructorfile) == instructor_fn: print( f"WARNING: Instructor file name is wrong {instructorfile} != {instructor_fn}" ) IP.display(IP.Javascript("IPython.notebook.save_notebook()"), include=['application/javascript']) studentfile = nbfilename(instructor_fn) if studentfile.isDate: tags['DUE_DATE'] = studentfile.getlongdate() tags['MMDD'] = studentfile.prefix self.removecells(searchstring="#ANSWER#", verbose=False) self.stripoutput() # Remove INSTRUCTOR from name studentfile.isInstructor = False self.filename = str(studentfile) tags['NEW_ASSIGNMENT'] = str(studentfile) print(tags['NEW_ASSIGNMENT']) self.mergetags(tags) student_fn = f"{studentfolder}{studentfile}" if Path(instructor_fn) == Path(student_fn): print("ERROR: student file will overrite instructor. Aborting") print(f" {instructor_fn} --> {student_fn}") return # Make a link for review IP.display( HTML(f"<a href={student_fn} target=\"blank\">{student_fn}</a>")) return student_fn
def test_in_class_fn(): filename = "0130_Software_Review_pre-class-assignment-INSTRUCTOR.ipynb" x = nbfilename(filename) new = x.makestring() assert (not new == filename) assert (x.isDate) assert (x.isInstructor) assert (not x.isInClass) assert (x.isPreClass)
def test_full_fn(): filename = "1010-This_is_a_test_in-class-INSTRUCTOR.ipynb" x = nbfilename(filename) new = x.makestring() assert (x.isDate) assert (x.isInstructor) assert (x.isInClass) assert (not x.isPreClass) assert (not new == filename)
def test_INSTRUCTOR_fn(): filename = "01-test-INSTRUCTOR.ipynb" x = nbfilename(filename) new = x.makestring() assert (new == filename) assert (not x.isDate) assert (x.isInstructor) assert (not x.isInClass) assert (not x.isPreClass)
def test_date_fn(): filename = "1212-test.ipynb" x = nbfilename(filename) new = x.makestring() assert (new == filename) assert (x.isDate) assert (not x.isInstructor) assert (not x.isInClass) assert (not x.isPreClass)
def makedateschedule(assignment_folder='assignments'): '''Make an index.md file inside the assignment_folder with references to html and ipynb files''' S_path = Path(assignment_folder) StudentFiles = S_path.glob(f"*.ipynb") nameset = set() for file in StudentFiles: nbfile = nbfilename(file) if nbfile.isDate: nameset.add(str(nbfile)) I_path = Path('.') InstructorFiles = I_path.glob(f"*.ipynb") schedule = "" schedule += "| Due Date | Assignment Number | Type | Topic | Link to Notebook |\n" schedule += "|------|--------|------|-------|----------|\n" for file in sorted(InstructorFiles): nbfile = nbfilename(str(file)) if (nbfile.isInstructor and nbfile.isDate): nbfile.isInstructor = False thisfile = str(nbfile) filetype = " " if nbfile.isPreClass: filetype = "Pre-Class Assignment" if nbfile.isInClass: filetype = "In-Class Assignment" nbfile.isInClass = False nbfile.isPreClass = False if thisfile in nameset: schedule += f"| {nbfile.getlongdate()}, 2021 | {nbfile.basename()[0:4]} | {filetype} | [{nbfile.basename()[5:]}](./{thisfile[:-6]}.html) | [ipynb](./{str(thisfile)}) |\n" else: schedule += f"| {nbfile.getlongdate()}, 2021 | {nbfile.basename()[0:4]} | {filetype} | {nbfile.basename()[5:].replace('_',' ')} |\n" indexfile = Path(assignment_folder, 'Schedule.md') # Read in the file with open(indexfile, 'w') as file: file.write(schedule) return str(indexfile)
def expandfiles(assignment, source='upziptemp', destination='AutoGrader/submitted'): nbfile = nbfilename(assignment) nbfile.isInstructor = False nbfile.isStudent = False sourcefolder = Path(source) ipynbfiles = sourcefolder.glob('*.ipynb') for thisfile in ipynbfiles: print(thisfile.stem) myfolder = Path(destination / Path(thisfile.stem) / Path(nbfile.basename())) myfolder.mkdir(parents=True, exist_ok=True) nbfile.isStudent = True thisfile.rename(myfolder / Path(assignment + nbfile.basename()))
def upload_D2L_submissions( instructorfile='HW1-Systems_of_linear_equations-INSTRUCTOR.ipynb', zipfile="./D2L/HW1-Test.zip"): import thiscourse from jupyterinstruct import InstructorNotebook tags = thiscourse.tags() nbfile = nbfilename(instructorfile) nbfile.isInstructor = True nbfile.isStudent = False studentnotebook = InstructorNotebook.makestudent(str(nbfile), "./CMSE314/", tags) nbstudentnb = importnb(studentnotebook) D2L_2_nbgrader(zipfile, str(nbstudentnb.name)) #Generate Temporary Grade file nbfile.isInstructor = False nbfile.isStudent = False command = f"echo pwd; cd AutoGrader; ls; nbgrader autograde {nbfile.basename()}" returned_output = subprocess.check_output(command, shell=True) print(f"Output: {returned_output.decode('utf-8')}")
def makecsvschedule( csvfile='CMSE314-001-NLA-S21_Schedule.csv', assignmentsfolder='./mth314-s21-student/assignments/', sections=["Section 001", "Section 002", "Section 003", "Section 004-005"], times=[ "Tu Th 10:20AM - 11:40AM", "M W 12:40PM - 2:00PM", "Tu Th 1:00PM - 2:20PM", "M W 12:40PM - 2:00PM" ]): df = pandas.read_csv(csvfile) webfolder = Path(assignmentsfolder) output = "" files = set() mdfiles = set() webfiles = set() for file in webfolder.glob('*.md'): mdfiles.add(str(file.name)) for file in webfolder.glob('*.html'): webfiles.add(str(file.name)) for file in webfolder.glob('*.ipynb'): files.add(str(file.name)) schedulefiles = [] for section, tm in zip(sections, times): schedule = f"# MTH314 {section} \n\n {tm}\n\n" schedule += "| Date | Assignment | Link to Notebook |\n" schedule += "|------|------------|------------------|\n" for i, row in df.iterrows(): file = row['Assignment'] if isinstance(file, str): if 'ipynb' in file: nbfile = nbfilename(file) nbfile.isInstructor = False schedule += f"| {row[section]} |" webname = f"{nbfile.basename()}.html" if webname in webfiles: schedule += f" [{nbfile.basename()}]({webname}) |" else: schedule += f" {nbfile.basename()} |" if str(nbfile) in files: schedule += f" [ipynb]({str(nbfile)}) |\n" else: schedule += f" |\n" else: webname = f"{file}.md" schedule += f"| {row[section]} |" if webname in mdfiles: schedule += f" [{file}]({webname}) | |\n" else: webname = f"{file}.html" if webname in webfiles: schedule += f" [{file}]({webname}) | |\n" else: schedule += f" {file} | |\n" name = section.replace(' ', '_') schedulefile = f"{assignmentsfolder}{name}.md" with open(schedulefile, "w") as file_object: file_object.write(schedule) schedulefiles.append(schedulefile) return schedulefiles