def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] ignore_case = bool(sys.argv[3]) if len(sys.argv) > 3 else True csv_teachers = qs.CSV(filename) q = qs.API(schoolcode) if not ('Teacher Name' in csv_teachers.cols): raise ValueError('Teacher Name column required.') db_teachers = q.get_teachers() db_duplicates = qs.find_dups_in_dict_list(db_teachers, 'fullName') if db_duplicates: qs.logger.critical('Teacher names are not unique in DB, duplicates:', db_duplicates) else: qs.logger.info('Teacher names are unique.') if ignore_case is True: for teacher in db_teachers: teacher['fullName'] = teacher['fullName'].lower() db_by_name = qs.dict_list_to_dict(db_teachers, 'fullName') teacher_names_not_matched = set() for csv_teacher in csv_teachers: csv_full_name = csv_teacher['Teacher Name'] if ignore_case: csv_full_name = csv_full_name.lower() db_match = db_by_name.get(csv_full_name) if db_match: csv_teacher['Teacher ID'] = db_match['id'] else: teacher_names_not_matched.add(csv_full_name) if teacher_names_not_matched: qs.logger.warning(('{} Teachers in the file were not found in the db' ''.format(len(teacher_names_not_matched))), teacher_names_not_matched) else: qs.logger.info('All teachers were matched in the db.', cc_print=True) filepath = qs.unique_path(csv_teachers.filepath, suffix="with teacher IDs") csv_teachers.save(filepath)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] q = qs.API(schoolcode) csv_report_cycles = qs.CSV(filename) if 'Report Cycle' not in csv_report_cycles.cols: raise ValueError("'Report Cycle' column required") db_report_cycles = q.get_report_cycles() db_duplicates = qs.find_dups_in_dict_list(db_report_cycles, 'name') db_by_name = qs.dict_list_to_dict(db_report_cycles, 'name') if db_duplicates: qs.logger.info('Report Cycle names are not unique', cc_print=True) else: qs.logger.info('Report Cycle names are unique', cc_print=True) report_cycle_names_not_matched = set() for csv_report_cycle in csv_report_cycles: csv_report_cycle_name = csv_report_cycle['Report Cycle'] if csv_report_cycle_name in db_duplicates: qs.logger.info('Report Cycle name has multiple matches', cc_print=True) db_match = db_by_name.get(csv_report_cycle_name) if db_match: csv_report_cycle['Report Cycle ID'] = db_match['id'] else: report_cycle_names_not_matched.add(csv_report_cycle_name) if report_cycle_names_not_matched: qs.logger.warning( ('{} Report Cycles in the file were not found in the db' ''.format(len(report_cycle_names_not_matched))), report_cycle_names_not_matched) else: qs.logger.info('All Report Cycles were matched in the db.', cc_print=True) filepath = qs.unique_path(csv_report_cycles.filepath, suffix="with-report-cycle-IDs") csv_report_cycles.save(filepath)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] q = qs.API(schoolcode) csv_enrollments = qs.CSV(filename) section_info = {} student_enrollments = {} # Get Section Info if 'Section ID' not in csv_enrollments.cols: qs.logger.critical('"Section ID" column required. Current columns: ', csv_enrollments.cols) if 'Student ID' not in csv_enrollments.cols: qs.logger.critical('"Student ID" column required. Current columns: ', csv_enrollments.cols) qs.logger.info('Setting up enrollment info from csv data...', cc_print=True) for enrollment in csv_enrollments: section_name = enrollment['Section Name'] section_id = enrollment['Section ID'] student_id = enrollment['Student ID'] if 'Section Code' in enrollment: section_code = enrollment['Section Code'] else: section_code = enrollment['Section Name'] if section_id not in student_enrollments: student_enrollments[section_id] = list() student_enrollments[section_id].append(student_id) section_info[section_id] = { 'section_name': section_name, 'section_id': section_id, 'section_code': section_code, 'student_ids': student_enrollments[section_id] } qs.logger.info('POSTing section enrollments...', cc_print=True) for section in qs.bar(section_info): section_id = section_info[section]['section_id'] students = section_info[section]['student_ids'] new_enrollment = q.post_section_enrollment(section_id, students)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] from_identifier = sys.argv[2] to_identifier = sys.argv[3] filename = sys.argv[4] q = qs.API(schoolcode) csv_section_values = qs.CSV(filename) sections = dict() empty_sections = list() if 'Student ID' not in csv_section_values.cols: raise ValueError("'Student ID' column required") elif 'Section ID' not in csv_section_values.cols: raise ValueError("'Section ID' column required") elif from_identifier not in csv_section_values.cols: raise ValueError("Identifier '{}'column is required" .format(from_identifier)) qs.logger.info("Retrieving section information and identifier values...", cc_print=True) for section in qs.bar(csv_section_values): student_id = section['Student ID'] section_id = section['Section ID'] identifier_values = section[from_identifier] if identifier_values is not None: if student_id not in sections: sections[student_id] = dict() sections[student_id][section_id] = {'values': {to_identifier: identifier_values}} else: if 'Full Name' in csv_section_values.cols and 'Section Name' in csv_section_values.cols: student_name = section['Student Name'] section_name = section['Section Name'] empty_sections.append({student_id: section_id, student_name: section_name}) else: empty_sections.append({student_id: section_id}) qs.logger.info("POSTing data to transcripts' sections...", cc_print=True) for student in qs.bar(sections): student_id = student transcript_data = sections[student] q.post_transcript_section_level(student_id, transcript_data)
def main(): filename = sys.argv[1] overwrite = qs.to_bool(sys.argv[2]) if len(sys.argv) > 2 else False csv = qs.CSV(filename) csv.cols.insert(csv.cols.index('Full Name') + 1, 'First') csv.cols.insert(csv.cols.index('Full Name') + 2, 'Last') for row in csv: full_name = row['Full Name'].strip() split_by_comma = full_name.split(',') split_by_space = full_name.split(' ') if len(split_by_comma) == 2: row['First'] = split_by_comma[1].strip() row['Last'] = split_by_comma[0].strip() elif len(split_by_space) == 2: row['First'] = split_by_space[0].strip() row['Last'] = split_by_space[1].strip() csv.save("with first last", overwrite=overwrite)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] server = sys.argv[2] filename = sys.argv[3] q = qs.API(schoolcode, server) csv_student_report_cycles = qs.CSV(filename) tr_enrollments = list() if 'Student ID' not in csv_student_report_cycles.cols: raise ValueError("'Student ID' column required.") for csv_student in qs.bar(csv_student_report_cycles): student_id = csv_student['Student ID'] if 'Full Name' in csv_student: full_name = csv_student['Full Name'] else: full_name = None tr_data = q.get_transcript(student_id)['sectionLevel'] for section in tr_data: section_id = section if full_name: tr_enrollments.append({'Student ID': student_id, 'Full Name': full_name, 'Section ID': section_id}) else: tr_enrollments.append({'Student ID': student_id, 'Section ID': section_id}) if tr_enrollments: qs.logger.info('Transcript section enrollment retrieved', cc_print=True) filepath = qs.unique_path(csv_student_report_cycles.filepath) qs.write_csv(tr_enrollments, filepath) else: qs.logger.info('No enrollments found for these students', cc_print=True)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] silent = bool(sys.argv[3]) if len(sys.argv) > 3 else False csv_sections = qs.CSV(filename) q = qs.API(schoolcode) row_num = 1 if 'Semester ID' in csv_sections.cols: qs.logger.info('Matching with db by Semester ID...', cc_print=True) for csv_section_info in qs.bar(csv_sections): section_name = csv_section_info['Section Name'] semester_id = csv_section_info['Semester ID'] if 'Section Code' in csv_sections.cols: section_code = csv_section_info['Section Code'] section = {'sectionCode': section_code} matched_section = q.match_section( section, fail_silent=silent, match_code=True, target_semester_id=semester_id) else: matched_section = q.match_section( identifier=section_name, fail_silent=silent, target_semester_id=semester_id) if matched_section is "FALSE": section_id = "FALSE" else: section_id = matched_section['id'] csv_section_info['Section ID'] = section_id elif 'Student ID' in csv_sections.cols: qs.logger.info('Matching with db by Student ID...', cc_print=True) for csv_section_info in qs.bar(csv_sections): section_name = csv_section_info['Section Name'] student = csv_section_info['Student ID'] row_num += 1 section = q.match_section(identifier=section_name, student_id=student, match_name=True) section_id = section['id'] csv_section_info['Section ID'] = section_id elif 'Section Code' in csv_sections.cols: qs.logger.info('Matching with db by Section Code...', cc_print=True) sections = {} sections_with_id = {} row_num = 1 for csv_section_info in csv_sections: section_name = csv_section_info['Section Name'] section_code = csv_section_info['Section Code'] row_num += 1 if section_code not in sections: sections[section_code] = {'sectionCode': section_code} qs.logger.info('GETting matching section ids by Section Code...', cc_print=True) for section_code in sections: matched_section = q.match_section(sections[section_code], match_code=True) section_id = matched_section['id'] if section_id not in sections_with_id: sections[section_code] = {'section_id': section_id} qs.logger.info('Matching section ids to csv section data...', cc_print=True) for csv_section_info in csv_sections: section_code = csv_section_info['Section Code'] csv_section_info['Section ID'] = sections[section_code][ 'section_id'] else: qs.logger.critical( '"Student ID", "Semester ID", or "Section Code" columns required. Current columns:', csv_sections.cols) # Make CSV only if successful if 'Section ID' in csv_section_info: filepath = qs.unique_path(csv_sections.filepath, suffix="with section IDs") csv_sections.save(filepath)
def main(): qs.logger.config(__file__) qs.logger.info('Setting up and opening file...',cc_print=True) key_1 = sys.argv[1] key_2 = sys.argv[2] filename = sys.argv[3] if len(sys.argv) > 4: disregard_value = sys.argv[4] csv_data_file = qs.CSV(filename) compared_rows = [] headers = [] successful_comparisons = 0 unsuccessful_comparisons = 0 # Check for required columns if not ('Student ID' or 'Teacher ID') in csv_data_file.cols: missing_columns = ['Student ID or Teacher ID'] if ('Teacher ID' in csv_data_file.cols) and ('Teacher Name'): missing_columns.append('Teacher Name') else: missing_columns.append('Full Name or First & Last') if missing_columns: for missing_column in missing_columns: print "Column Missing: {}" . format(missing_column) sys.tracebacklimit = 0 raise ValueError("Columns are missing from CSV. See above for details.") # Make headers for output file if 'Student ID' in csv_data_file.cols: headers.append('Student ID') else: headers.append('Teacher ID') if 'Full Name' in csv_data_file.cols: headers.append('Full Name') elif 'Teacher Name' in csv_data_file.cols: headers.append('Teacher Name') else: headers.append('First') headers.append('Last') headers.append(key_1) headers.append(key_2) headers.append('Comparison') # Actually compare things qs.logger.info('Comparing values...', cc_print=True) for row in qs.bar(csv_data_file): if 'Student ID' in csv_data_file.cols: person_id = row['Student ID'] id_type = 'Student ID' if 'Full Name' in csv_data_file.cols: name = row['Full Name'] name_type = 'Full Name' else: first = row['First'] last = row['Last'] else: person_id = row['Teacher ID'] name_type = 'Teacher ID' name = row['Teacher Name'] name_type = 'Teacher Name' key_1_value = row[key_1] key_2_value = row[key_2] comparison = '' if key_1_value == disregard_value: comparison = 'TRUE' successful_comparisons += 1 elif key_2_value == disregard_value: comparison = 'TRUE' successful_comparisons += 1 else: if key_1_value == key_2_value: comparison == 'TRUE' successful_comparisons += 1 else: comparison == 'FALSE' unsuccessful_comparisons += 1 if ('First' and 'Last') in csv_data_file.cols: new_row = {id_type: person_id, 'First': first, 'Last': last, key_1: key_1_value, key_2: key_2_value, 'Comparison': comparison} else: new_row = {id_type: person_id, name_type: name, key_1: key_1_value, key_2: key_2_value, 'Comparison': comparison} qs.logger.info(new_row) compared_rows.append(new_row) qs.logger.info('Comparison Complete', cc_print=True) qs.logger.info('Successful Comparisons: ', successful_comparisons, cc_print=True) qs.logger.info('Unsuccessful Comparisons: ', unsuccessful_comparisons, cc_print=True) new_filepath = filepath.rstrip('.csv') + ('_processed.csv') qs.write_csv(compared_rows, new_filepath, column_headers=headers)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] q = qs.API(schoolcode) csv_student_report_cycles = qs.CSV(filename) rc_enrollments = list() if 'Student ID' not in csv_student_report_cycles.cols: raise ValueError("'Student ID' column required.") if 'Report Cycle ID' not in csv_student_report_cycles.cols: raise ValueError("'Report Cycle ID' column required.") for csv_student in qs.bar(csv_student_report_cycles): student_id = csv_student['Student ID'] report_cycle_id = csv_student['Report Cycle ID'] if 'Full Name' in csv_student: full_name = csv_student['Full Name'] else: full_name = None if 'Report Cycle' in csv_student: report_cycle = csv_student['Report Cycle'] else: report_cycle = None rc_section_data = q.get_report_card(student_id, report_cycle_id)['sectionLevel'] for section in rc_section_data: section_id = section if full_name: if report_cycle: rc_enrollments.append({ 'Student ID': student_id, 'Full Name': full_name, 'Report Cycle ID': report_cycle_id, 'Report Cycle': report_cycle, 'Section ID': section_id }) else: rc_enrollments.append({ 'Student ID': student_id, 'Full Name': full_name, 'Report Cycle ID': report_cycle_id, 'Section ID': section_id }) else: if report_cycle: rc_enrollments.append({ 'Student ID': student_id, 'Report Cycle ID': report_cycle_id, 'Report Cycle': report_cycle, 'Section ID': section_id }) else: rc_enrollments.append({ 'Student ID': student_id, 'Report Cycle ID': report_cycle_id, 'Section ID': section_id }) if rc_enrollments: qs.logger.info('Report Card section enrollment retrieved', cc_print=True) filepath = qs.unique_path(csv_student_report_cycles.filepath) qs.write_csv(rc_enrollments, filepath) else: qs.logger.info( 'No enrollments found for these students and report cylces', cc_print=True)
def main(): for filepath in sys.argv[1:]: csv = qs.CSV(filepath) new_filepath = filepath.replace('csv', 'json') json_filepath = csv.save_as_json(new_filepath) print "Saved {} as a JSON file:\n{}".format(filepath, json_filepath)
def main(): qs.logger.config(__file__) schoolcode = sys.argv[1] filename = sys.argv[2] q = qs.API(schoolcode) csv_grades = qs.CSV(filename) sections = {} grades = {} new_assignments = [] required_columns = ['Section ID', 'Student ID', 'Total Pts', 'Category ID', 'Marks', 'Grading Scale ID'] missing_columns = [] for required_column in required_columns: if required_column not in csv_grades.cols: missing_columns.append(required_column) if missing_columns: for missing_column in missing_columns: print "Column Missing: {}" . format(missing_column) sys.tracebacklimit = 0 raise ValueError("Columns are missing from CSV. See above for details.") # Make dict of students' assignment grades and assignment metadata for student_section_record in csv_grades: if 'Assignment Name' in student_section_record: assign_name = student_section_record[u'Assignment Name'] else: assign_name = 'Grade Import' if 'Assignment Date' in student_section_record: assign_date = student_section_record['Assignment Date'] else: assign_date = qs.today() section = student_section_record['Section ID'] cat_id = student_section_record['Category ID'] total = student_section_record['Total Pts'] grade_scale = student_section_record['Grading Scale ID'] marks = student_section_record['Marks'] student = student_section_record['Student ID'] # List out students' assignment grades if section not in grades: grades[section] = dict() if assign_name not in grades[section]: grades[section][assign_name] = list() grades[section][assign_name].append({'studentId': student, 'marks': marks}) # List out assignment metadata if section not in sections: sections[section] = dict() sections[section][assign_name] = {'cat_id': cat_id, 'total': total, 'grade_scale': grade_scale, 'assign_date': assign_date, 'assign_name': assign_name, 'section_id': section, 'grades_data': []} for section in sections: for assign_name in sections[section]: sections[section][assign_name]['grades_data'] = grades[section][assign_name] qs.logger.info(sections) # POST assignment and POST grades to it qs.logger.info('POSTing assignments...', cc_print=True) for section in qs.bar(sections): for assign_name in sections[section]: assign_data = sections[section][assign_name] section_id = assign_data['section_id'] new_grade = q.post_assignment_with_grades(section, assign_data['assign_name'], assign_data['assign_date'], assign_data['total'], assign_data['cat_id'], assign_data['grade_scale'], assign_data['grades_data']) qs.logger.info('--> NEW ASSIGNMENT ID: ', new_grade) qs.logger.info('--> NEW SECTION ID: ', section_id) new_assignments.append({'Assignment ID': new_grade, 'Section ID': section_id}) qs.logger.info('All Assignments are posted....', cc_print=True) filepath = qs.unique_path(csv_grades.filepath, suffix='_posted_asignments') qs.write_csv(new_assignments, filepath)