def read_student(self): str_student = raw_input("New student(registration number,name): ") str_student = str_student.split(",") if len(str_student) != 2: raise StudentError("Not a student") try: id = int(str_student[0]) except ValueError: raise ValueError("Registration number must be an integer") name = str_student[1] return id, name
def find(self, id): ''' find -> Student Returns the Student from the list of students that has the same id as the student given, if it exists, if not, raises StudentError :param id: int ''' s = self.find_by_id(id) if s != -1: return s else: raise StudentError("Student not found")
def update(self, student): ''' update_course -> None Searches if the given student id exists in the list of Students, if it exists, replaces the existing student with the given student If the id doesn't exist, raises StudentError :param student: Student ''' index = self.find_index(student) if index != -1: self.students[index] = student else: raise StudentError("Student not found")
def save(self, student): ''' save -> None Searches if the given student id exists in the list of Students, if not adds the student to the list If the id already exists, raises StudentError :param student: Student ''' index = self.find_index(student) if index == -1: self.students.append(student) else: raise StudentError("Duplicated id's")
def delete(self, id): ''' delete -> None Searches if the given id exists in the list of Students, if it exists, deletes the student with the given id If the id doesn't exist, raises StudentError :param id: int ''' s = self.find_by_id(id) if s != -1: index = self.find_index(s) del self.students[index] else: raise StudentError("Student not found")
def find_by_name(self, name): ''' find_by_name -> list of Students Returns the list of students with the given name, raises StudentError if there isn't one :param name: string ''' students = [] for i in range(0, len(self.students)): if name == self.students[i].get_name(): students.append(self.students[i]) if students == []: raise StudentError("No student found") return students
def validate_name(name): errors = "" if name == "": errors += "Name must be introduced" else: char = name[0] char = char.upper() if name[0] != char: errors += "Name must start with a capital letter\n" if not(name.isalpha()): errors += "Name must contain only letters\n" for i in range(1,len(name)): char = name[i] char = char.lower() if name[i] != char: errors += "Name letters must be lowercase(except the first)" break if len(errors) > 0: raise StudentError(errors)