def setUp(self):
     student_names = ["Bob", "Joe", "Muhammad", "Rosa", "Annie"]
     course_names = ["CSC148", "MAT157"]
     self.school = School()
     for name in student_names:
         self.school.create_student(name)
     for name in course_names:
         self.school.create_course(name)
def get_all_schools():
    schools = {}
    for line in f:
        line = line.split(",")
        if str(line[1]).strip() != 'GEN':
            name = str(line[0]).strip() + str(line[1]).strip()
        else:
            name = str(line[0]).strip()
        cap = int(line[2])
        print name, cap
        s = School(name, cap)
        schools[s] = s
    return schools
Exemple #3
0
 def __init__(self):
     self.school = School()
     self.undo_stack = Stack()
Exemple #4
0
class Interface:
    """The interface class
    """

    def __init__(self):
        self.school = School()
        self.undo_stack = Stack()

    def run(self):
        """(Interface) -> NoneType
        Runs an infinite loop to gather input from the user, and execute that input.
        Input is subsequently stored in self.undo_stack (unless the last command was undo).
        Failed commands are noted as such in undo_stack.
        """
        while True:
            input_tuple = self.get_input()
            success = self.execute_input(*input_tuple)

            if not input_tuple[0] == 'undo':

                if success is False:
                    self.undo_stack.push(tuple([False]))

                elif success is None:
                    break

                else:
                    self.undo_stack.push(input_tuple)


    def get_input(self):
        """(Interface) -> tuple of strings
        Prompts the user for input, splits the input, and returns a tuple of the split strings.
        Exception: If the user inputs 'create student <name>', a tuple containing ('create student', <name>).
        """
        user_input = input('Input command: ')
        split_input = [False]
        if user_input:
            split_input = user_input.split()

        if split_input[0] == 'create' and split_input[1] == 'student':
            return tuple(['create student', split_input[2]])

        else:
            return tuple(split_input)

    def execute_input(self, command, argument_1=None, argument_2=None):
        """(str, str, str) -> bool
        Executes the methods that correspond to a given valid command.
        Returns None when the command 'exit' is input.
        Prints and error message and returns False if an invalid command is given.
        """
        if self.is_valid_command(command):

            if command == 'create student':
                return self.create_student(argument_1)

            elif command == 'enrol':
                return self.enrol(argument_1, argument_2)

            elif command == 'drop':
                return self.drop(argument_1, argument_2)

            elif command == 'list-courses':
                return self.list_courses(argument_1)

            elif command == 'common-courses':
                return self.common_courses(argument_1, argument_2)

            elif command == 'class-list':
                return self.class_list(argument_1)

            elif command == 'exit':
                return None

            elif command == 'undo':
                return self.undo_n(argument_1)

        else:
            print('Unrecognized command!')
            return False

    def is_valid_command(self, command):
        """(Interface, str) -> bool
        Helper method for execute_input. Verifies that a command is valid.
        """

        valid_commands = {'create student', 'enrol', 'drop', 'list-courses', 'common-courses', 'class-list', 'exit',
                          'undo'}

        return command in valid_commands

    def undo_n(self, n):
        """(Interface, str) -> NoneType
        Runs undo n times.
        If no n is given, n defaults to 1.
        Prints an error message if n is not a positive natural number.
        """

        if n is None:
            n = 1

        try:
            n = int(n)

            if n >= 1:
                for x in range(n):
                    success = self.undo()
                    if not success:
                        break

            else:
                print('ERROR: %s is not a positive natural number.' % n)

        except ValueError:
            print('ERROR: %s is not a positive natural number.' % n)

    def undo(self):
        """(Interface, int) -> bool
        Undoes the last command then returns True.
        Prints an error message and returns False if there are no more commands to undo.
        """

        undoable_commands = {'create student', 'enrol', 'drop'}

        try:
            last_command = self.undo_stack.pop()

            if last_command[0] and last_command[0] in undoable_commands:

                if last_command[0] == 'create student':
                    self.school.delete_student(last_command[1])

                if last_command[0] == 'enrol':
                    self.drop(*last_command[1:])

                if last_command[0] == 'drop':
                    self.enrol(*last_command[1:])

            return True

        except IndexError:
            print('ERROR: No commands to undo.')
            return False

    def create_student(self, name):
        """(Interface, str) -> bool
        Creates a new Student named name in self.school then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            self.school.create_student(name)
            return True

        except RegistrationError as e:
            e.print_err_msg(name)
            return False

    def enrol(self, student_name, course_name):
        """(Interface, str, str) -> bool
        Enrolls a student in a course then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            self.school.enrol(student_name, course_name)
            return True

        except RegistrationError as e:
            e.print_err_msg(student_name)
            return False

    def drop(self, student_name, course_name):
        """(Interface, str, str) -> bool
        Drops a student from a course then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            self.school.drop_course(student_name, course_name)
            return True

        except RegistrationError as e:
            e.print_err_msg(student_name)
            return False

    def list_courses(self, student_name):
        """(Interface, str) -> bool
        Prints a student's courses then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            courses = self.school.get_course_list(student_name)
            print(', '.join(courses))

            return True

        except RegistrationError as e:
            e.print_err_msg(student_name)
            return False

    def common_courses(self, student_1_name, student_2_name):
        """(Interface, str, str) -> bool
        Prints a list of the courses common between two named students then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            course_list = self.school.common_courses(student_1_name, student_2_name)
            print(', '.join(course_list))

            return True

        except RegistrationError as e:
            if e.code == 2001:
                e.print_err_msg(student_1_name)
            elif e.code == 2002:
                e.print_err_msg(student_2_name)
            elif e.code == 2003:
                e.print_err_msg(student_1_name)
                e.print_err_msg(student_2_name)

            return False

    def class_list(self, course_name):
        """(Interface, str) -> bool
        Prints a list of all students in a certain course then returns True.
        Prints an error message and returns False if an error is raised.
        """
        try:
            class_list = self.school.get_all_enrolled(course_name)
            print(', '.join(class_list))

            return True

        except RegistrationError as e:
            e.print_err_msg(course_name)
            return False
class TestSchool(unittest.TestCase):
    def setUp(self):
        student_names = ["Bob", "Joe", "Muhammad", "Rosa", "Annie"]
        course_names = ["CSC148", "MAT157"]
        self.school = School()
        for name in student_names:
            self.school.create_student(name)
        for name in course_names:
            self.school.create_course(name)

    def test_create_student(self):
        name = "Bob"
        student = Student(name)
        self.assertTrue(self.school.students[name], student)

    def test_create_student_already_exists(self):
        with self.assertRaises(RegistrationError):
            self.school.create_student("Bob")

    def test_create_course(self):
        self.school.create_course("MAT157")
        self.assertTrue(self.school.courses["MAT157"], Course("MAT157"))

    def test_enrol_and_implicit_create_course(self):
        student_name = "Joe"
        course_name = "MAT223"
        self.school.create_course(course_name)
        self.school.enrol(student_name, course_name)
        self.assertEqual(self.school.get_all_enrolled(course_name), [student_name])
        self.assertEqual(self.school.get_course_list(student_name), [course_name])

    def test_normal_enrollment(self):
        self.school.enrol("Bob", "CSC148")
        self.school.enrol("Joe", "CSC148")
        self.school.enrol("Rosa", "CSC148")
        self.assertEqual(self.school.get_all_enrolled("CSC148"), ["Bob", "Joe", "Rosa"])

    def test_normal_drop(self):
        student_m = "Muhammad"
        student_2 = "Annie"
        course_m = "CSC148"
        self.school.enrol(student_m, course_m)
        self.school.enrol(student_2, course_m)
        self.school.drop_course(student_m, course_m)
        self.assertEqual(self.school.get_all_enrolled("CSC148"), ["Annie"])

    def test_empty_drop(self):
        course_m = "CSC148"
        student_m = "Lewis"
        with self.assertRaises(RegistrationError):
            self.school.drop_course(student_m, course_m)

    def test_list_courses(self):
        student = "Bob"
        self.school.enrol(student, "CSC148")
        self.school.enrol(student, "MAT157")
        self.assertEqual(self.school.get_course_list(student), ["CSC148", "MAT157"])

    def test_error_list_courses(self):
        student = "Lewis"
        with self.assertRaises(RegistrationError):
            self.school.get_course_list(student)

    def test_normal_common_courses(self):
        student_1 = "Bob"
        student_2 = "Rosa"
        self.school.enrol("Rosa", "CSC148")
        self.school.enrol("Rosa", "MAT157")
        self.school.enrol("Bob", "CSC148")
        self.assertEqual(self.school.common_courses(student_1, student_2), ["CSC148"])

    def test_empty_common_courses(self):
        student_1 = "Bob"
        student_2 = "Rosa"
        self.school.enrol("Rosa", "MAT157")
        self.school.enrol("Bob", "CSC148")
        self.assertEqual(self.school.common_courses(student_1, student_2), [])

    def test_error_common_courses(self):
        student_1 = "Bob"
        student_2 = "Lewis"
        with self.assertRaises(RegistrationError):
            self.school.common_courses(student_1, student_2)

    def test_class_list(self):
        student_1 = "Bob"
        student_2 = "Rosa"
        self.school.enrol(student_1, "MAT157")
        self.school.enrol(student_2, "MAT157")
        self.assertEqual(self.school.get_all_enrolled("MAT157"), ["Bob", "Rosa"])