diff options
Diffstat (limited to 'mit/extented_class_example')
| -rw-r--r-- | mit/extented_class_example/GradeBook.py | 94 | ||||
| -rw-r--r-- | mit/extented_class_example/GradeBook.pyc | bin | 0 -> 1000 bytes | |||
| -rw-r--r-- | mit/extented_class_example/MITPerson.py | 83 | ||||
| -rw-r--r-- | mit/extented_class_example/MITPerson.pyc | bin | 0 -> 3009 bytes | |||
| -rw-r--r-- | mit/extented_class_example/Person.py | 38 | ||||
| -rw-r--r-- | mit/extented_class_example/Person.pyc | bin | 0 -> 1612 bytes |
6 files changed, 215 insertions, 0 deletions
diff --git a/mit/extented_class_example/GradeBook.py b/mit/extented_class_example/GradeBook.py new file mode 100644 index 0000000..36c25fb --- /dev/null +++ b/mit/extented_class_example/GradeBook.py @@ -0,0 +1,94 @@ +#!/usr/bin/python3 + +from MITPerson import * + +class Grades(object): + """ A mapping from students to a list of grades """ + def __init__(self): + """ Create empty grade book """ + self.students = [] # List of student objects + self.grades = {} # maps idNum -> list of grades + self.isSorted = True # true if self.students is sorted + + def addStudent(self, student): + """ + Assumes: student is of type Student + Add student to the grade book + """ + if student in self.students: + raise ValueError('Duplicated student') + + self.students.append(student) + self.grades[student.getIdNum()] = [] # dict is a ID:List pair + self.isSorted = False + + def addGrade(self, student, grade): + """ + Assumes: grade is a float + Add grade to the list of grades for student + """ + try: + self.grades[student.getIdNum()].append(grade) + except KeyError: + raise ValueError('Student not in grade book') + + def getGrades(self, student): + """ Return a list of grades for student """ + try: # Return a COPY of student's grades + return self.grades[student.getIdNum()][:] + except KeyError: + raise ValueError('Student not in grade book') + + def allStudents(self): + """ Return a list of the students in the grade book """ + if not self.isSorted: + self.students.sort() + self.isSorted = True + return self.students[:] # Return a COPY of students, so we avoid any + # externalmodification to the original list + + + +### TESTS + +def gradeReport(course): + """ Assumes: course is of type grades """ + + report = [] + + for s in course.allStudents(): + tot = 0.0 + numGrades = 0 + + for g in course.getGrades(s): + tot += g + numGrades += 1 + + try: + mean = tot/numGrades + report.append(str(s) + '\'s mean grade is ' + str(mean)) + except ZeroDivisionError: + report.append(str(s) + ' has no grades') + + return ' \n'.join(report) + + +six00 = Grades() +ug3 = UG('Drew Houston', 2017) +ug1 = UG('Matt Damon', 2018) +ug2 = UG('Ben Affleck', 2019) +ug4 = UG('Mark Zuckerberg', 2017) +g1 = Grad('Bill Gates') +g2 = Grad('Steve Wozniak') + +six00.addStudent(g1) +six00.addStudent(ug2) +six00.addStudent(ug1) +six00.addStudent(g2) +six00.addStudent(ug4) +six00.addStudent(ug3) + +six00.addGrade(g1, 90) +six00.addGrade(g2, 45) +six00.addGrade(ug1, 80) +six00.addGrade(ug2, 75) diff --git a/mit/extented_class_example/GradeBook.pyc b/mit/extented_class_example/GradeBook.pyc Binary files differnew file mode 100644 index 0000000..2762139 --- /dev/null +++ b/mit/extented_class_example/GradeBook.pyc diff --git a/mit/extented_class_example/MITPerson.py b/mit/extented_class_example/MITPerson.py new file mode 100644 index 0000000..b6bc856 --- /dev/null +++ b/mit/extented_class_example/MITPerson.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 + +from Person import * + +class MITPerson(Person): + + nextIdNum = 0 # next ID number to assign + + def __init__(self, name): + Person.__init__(self, name) # Initialize person's attributes + self.idNum = MITPerson.nextIdNum # MITPerson global attribute: unique ID + MITPerson.nextIdNum += 1 + + def getIdNum(self): + return self.idNum + + # Sorting MIT people uses their ID number, not name + def __lt__(self, other): + return self.idNum < other.idNum + + def speak(self, utterance): + return (self.name + " says: " + utterance) + +class Student(MITPerson): + pass + +class UG(Student): + def __init__(self, name, classYear): + MITPerson.__init__(self, name) + self.year = classYear + + def getClass(self): + return self.year + + def speak(self, utterance): + return MITPerson.speak(self, " Yo Bro: " + utterance) + +class Grad(Student): + pass + +class TransferStudent(Student): + pass + + + +# By creating a new 'blank' class Student, and making all UG, Grad and +# TransferStudent classes inherit from it, we clean up the class inheritance a +# bit, so, in the function below, we can simply us Student class, to check if an +# object belongs to that class, instead of the old version (commented out below, +# for historical perspective) +def isStudent(obj): +# return isinstance(obj, UG) or isinstance(obj, Grad) + return isinstance(obj, Student) + +class Professor(MITPerson): + def __init__(self, name, department): + MITPerson__init__(self, name) + self.department = department + + def speak(self, utterance): + new = "In course " + self.department + " we say " + return MITPerson.speak(self, new + utterance) + + def lecture(self, topic): + return self.speak('it is obvious that ' + topic) + +m1 = MITPerson("Mark Zuckeberg") +m2 = MITPerson("Bill Gates") +m3 = MITPerson("Carlos Maiolino") +p1 = Person("Drew Houston") + + +s1 = UG("Matt Damon", 2017) +s2 = Grad("Leonardo di Caprio") +# Remember, comparison is done by using __lt__ method, which takes the instance +# itself + another instance. When using the MITPerson instance, the __lt__ +# method uses the idNum attribute, which, an instance of Person class will not +# have. But, using an instance of Person class, the __lt__ method, uses the +# LastName, which, since MITPerson is a subclass of Person class, it also +# contains a lastName attribute + +## print(p1 < m3) # Comparison works +## print(m3 < p1) # Comparison does not work (Attribute error) diff --git a/mit/extented_class_example/MITPerson.pyc b/mit/extented_class_example/MITPerson.pyc Binary files differnew file mode 100644 index 0000000..3bc833c --- /dev/null +++ b/mit/extented_class_example/MITPerson.pyc diff --git a/mit/extented_class_example/Person.py b/mit/extented_class_example/Person.py new file mode 100644 index 0000000..481dd69 --- /dev/null +++ b/mit/extented_class_example/Person.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +import datetime + +class Person(object): + def __init__(self, name): + """Create a person called name""" + self.name = name + self.birthday = None + self.lastName = name.split(' ')[-1] + + def getLastName(self): + """return self's last name""" + return self.lastName + + def __str__(self): + """return self's name""" + return self.name + + + def setBirthday(self,month,day,year): + """sets self's birthday to birthDate""" + self.birthday = datetime.date(year,month,day) + + def getAge(self): + """returns self's current age in days""" + if self.birthday == None: + raise ValueError + return (datetime.date.today() - self.birthday.days()) + + def __lt__(self, other): + """ + return True if self's name is lexicographically less than other's + name, and False otherwise + """ + if self.lastName == other.lastName: + return self.name < other.name + return self.lastName < other.lastName diff --git a/mit/extented_class_example/Person.pyc b/mit/extented_class_example/Person.pyc Binary files differnew file mode 100644 index 0000000..17f7ef7 --- /dev/null +++ b/mit/extented_class_example/Person.pyc |
