def students_to_queue(filename, q_type): """ ------------------------------------------------------- Description: Converts students stored in a file in a queue if q_type is cQueue --> use cQueue if q_type is pQueue --> use pQueue('H') otherwise, print error and return None Use: q = students_to_queue(filename,'cQueue') q = students_to_queue(filename, 'pQueue') ------------------------------------------------------- Parameters: filename: name of file containing students (str) q_type: cQueue or pQueue (str) Returns: q: a Queue containing students objects (cQueue or pQueue) ------------------------------------------------------- """ fv = open(filename, 'r') lines = fv.readlines() size = len(lines) if q_type == 'cQueue': q = cQueue(size) for line in lines: int1 = line.find(',') sid = line[0:7] last = line[8:int1] first = line[int1 + 1:].strip('\n') student = Student(sid, last, first) q.insert(student) elif q_type == 'pQueue': q = pQueue(size, mode='H') for line in lines: line.strip('\n') int1 = line.find(',') sid = line[0:7] last = line[8:int1] first = line[int1 + 1:].strip('\n') student = Student(sid, last, first) q.insert(student) else: print('Error(file_to_queue): unsupported q_type') q = None fv.close() return q
def file_to_students(filename): """ ------------------------------------------------------- Description: Reads contents of given file into a list of Student Objects Each line in file is formatted as: sid:last,first Use: students = file_to_students(filename) ------------------------------------------------------- Parameters: filename - name of file (str) Returns: students - list of student objects (list) ------------------------------------------------------- """ students = [] f = open(filename, 'r') for line in f: int1 = line.find(':') int2 = line.find(',') sid = line[:int1] last = line[int1 + 1:int2] first = line[int2 + 1:].strip('\n') student = Student(sid, last, first) students.append(student) return students
def file_to_course(filename): """ ------------------------------------------------------- Description: Reads contents of given file into a Course object First line in the file contains course CRN The following lines are formatted as: sid:last,first Use: course = file_to_course(filename) ------------------------------------------------------- Parameters: filename - name of file (str) Returns: course - a course object (Course) ------------------------------------------------------- """ f = open(filename, 'r') crn = f.readline().strip('\n') students = [] for line in f: int1 = line.find(':') int2 = line.find(',') sid = line[:int1] last = line[int1 + 1:int2] first = line[int2 + 1:].strip('\n') student = Student(sid, last, first) students.append(student) course = Course(crn, students) return course
def main(): serializers = {"json": JSONSerializer, "xml": XMLSerializer} command_reader = ConsoleReader() parameters = command_reader.read_from_command_line() output_file_name = "rooms_with_students" parser = JSONParser() students_path = parser.create_data_path(parameters.get("students"), "students.json") dict_students = parser.extract_text(students_path) students = {} for student in dict_students: students.setdefault(student.get("room"), []) students[student.get("room")].append(Student.student_mapper(student)) rooms_path = parser.create_data_path(parameters.get("students"), "rooms.json") dict_rooms = parser.extract_text(rooms_path) rooms = [] for room in dict_rooms: rooms.append(Room.room_mapper(room)) for room in rooms: room.add_students(students[room.id]) rooms = [room.to_dict() for room in rooms] serializer = serializers.get(parameters.get("format")) if parameters.get("format") == "xml": serialized_rooms = serializer("rooms").serialize(rooms) else: serialized_rooms = serializer().serialize(rooms) saver = FileSaver(parameters.get("rooms"), output_file_name, parameters.get("format"), "w") saver.save(serialized_rooms)
def stack_to_students(stack): """ ------------------------------------------------------- Description: Copy students stored in a stack into a list of students Should not access the items directly, need to use push/pop After copy operation, both stack and list should have a distinct copy of students The top of the stack will be the last item in the list if input is not a stack, the function prints an error message Use: students = stack_to_students(stack) ------------------------------------------------------- Parameters: stack - a stack containing student objects (Stack) Returns: students - a list of students (list) ------------------------------------------------------- """ students = [] s = deepcopy(stack) if count_stack(s) % 3 == 0 and isinstance(s, Stack): while not s.is_empty(): sid = str(s.pop()) last = str(s.pop()) first = str(s.pop()) student = Student(sid,last,first) students.append(student) else: print('Error(stack_to_students): Invalid stack') return students
def students_to_stack(filename, size): """ ------------------------------------------------------- Description: Reads the contents of a file into a stack The file stores student information, each in separate line sid:last,first The stack is initialized with the given size The function does not perform file reading if stack is full Use: stack = students_to_stack(filename,size) ------------------------------------------------------- Parameters: filename - name of input file (str) size - Maximum stack size (int) Returns: stack - a stack containing student objects (Stack) ------------------------------------------------------- """ i = 0 stack = Stack(size) file = open(filename, 'r') for line in file: int1 = line.find(',') sid = line[:7] last = line[8:int1] first = line[int1 + 1:].strip('\n') student = Student(sid, last, first) if i != size: stack.push(student) i += 1 return stack
def create_student(): student_j = request.get_json() if "id" in student_j: abort(422) session = get_session() try: print("HERE") student = Student.from_dict(student_j) session.add(student) session.commit() except () as error: abort(422, error) return jsonify(student.asdict()), 201
def __init__(self, id, hospitals): Student.__init__(self, id) np.random.shuffle(hospitals) self.priorities = np.copy(hospitals)