def load_data(file_path, teachers_empty_space, groups_empty_space, subjects_order): """ Loads and processes input data, initialises helper structures. :param file_path: path to file with input data :param teachers_empty_space: dictionary where key = name of the teacher, values = list of rows where it is in :param groups_empty_space: dictionary where key = group index, values = list of rows where it is in :param subjects_order: dictionary where key = (name of the subject, index of the group), value = [int, int, int] where ints represent start times (row in matrix) for types of classes P, V and L respectively. If start time is -1 it means that that subject does not have that type of class. :return: Data(groups, teachers, classes, classrooms) """ with open(file_path) as file: data = json.load(file) # classes: dictionary where key = index of a class, value = class classes = {} # classrooms: dictionary where key = index, value = classroom name classrooms = {} # teachers: dictionary where key = teachers' name, value = index teachers = {} # groups: dictionary where key = name of the group, value = index groups = {} class_list = [] for cl in data['Time']: new_group = cl['Group'] new_teacher = cl['Teacher'] # initialise for empty space of teachers if new_teacher not in teachers_empty_space: teachers_empty_space[new_teacher] = [] new = Class(new_group, new_teacher, cl['Subject'], cl['Type'], cl['Duration'], cl['Classroom']) # add groups for group in new_group: if group not in groups: groups[group] = len(groups) # initialise for empty space of groups groups_empty_space[groups[group]] = [] # add teacher if new_teacher not in teachers: teachers[new_teacher] = len(teachers) class_list.append(new) # shuffle mostly because of teachers random.shuffle(class_list) # add classrooms for cl in class_list: classes[len(classes)] = cl # every class is assigned a list of classrooms he can be in as indexes (later columns of matrix) for type in data['Classrooms']: for name in data['Classrooms'][type]: new = Classroom(name, type) classrooms[len(classrooms)] = new # every class has a list of groups marked by its index, same for classrooms for i in classes: cl = classes[i] classroom = cl.classrooms index_classrooms = [] # add classrooms for index, c in classrooms.items(): if c.type == classroom: index_classrooms.append(index) cl.classrooms = index_classrooms class_groups = cl.groups index_groups = [] for name, index in groups.items(): if name in class_groups: # initialise order of subjects if (cl.subject, index) not in subjects_order: subjects_order[(cl.subject, index)] = [-1, -1, -1] index_groups.append(index) cl.groups = index_groups return Data(groups, teachers, classes, classrooms)
titlenode = utils.getFirstDescendant(sub, "class-title") titletext = getTitleText(titlenode) # create a new subsection subsection = Subsection(subsectionsymbol, titletext) # many to one rela section.subsections.append(subsection) classlist = sub.findall('classification-item') for clas in classlist: classsymbol = clas.find("classification-symbol").text classtitle = clas.find('class-title') classdesc = getTitleText(classtitle) print classdesc newclass = Class(classsymbol, classdesc) subclasslist = clas.findall('classification-item') for subclass in subclasslist: subclasssymbol = subclass.find('classification-symbol').text subclasstitle = subclass.find('class-title') subclassdesc = getTitleText(subclasstitle) newsubclass = Subclass(subclasssymbol, subclassdesc) session.add(newsubclass) newclass.subclasses.append(newsubclass) session.add(newclass) subsection.classes.append(newclass) session.add(subsection)
def enter_courses_into_db(): for id, name in COURSES.items(): cl = Class(id=id, name=name) db.add(cl) db.commit()
import contextlib from model import User, Class, Tutor, Registered, Tutoring, Base from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import MetaData engine = create_engine('sqlite:///sqlalchemy_example.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) db = DBSession() tutors = db.query(Tutor).filter_by(class_id='CSE128').all() print(tutors) test_user = User(id=1, name='Priya', email='*****@*****.**') test_user1 = User(id=2, name='Austin', email='*****@*****.**') test_class = Class(id='CS128', name='CS128') test_class1 = Class(id='CS133', name='CS133') test_tutor = Tutor(id=1, name='Priya', class_id='CSE128') test_tutor1 = Tutor(id=2, name='Austin', class_id='CSE133') test_tutor3 = Tutor(id=3, name='zoowee', class_id='CSE128') db.add(test_user) db.add(test_user1) db.add(test_class) db.add(test_class1) db.add(test_tutor) db.add(test_tutor1) db.add(test_tutor3) db.commit() tutors = db.query(Tutor).filter_by(class_id='CSE128').all() print(tutors)