Last active
October 4, 2016 17:53
-
-
Save xpostudio4/4d7e1711807f8ced3c61e822d9cb96e0 to your computer and use it in GitHub Desktop.
Refactoring: Growing Software like a Gardener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleGradebook(object): | |
def __init__(self): | |
self.__grades = {} | |
def add_student(self, name): | |
self._grades[name] = [] | |
def report_grade(self, name, score): | |
self._grades[name].append(score) | |
def average_grade(self, name): | |
grades = self._grades[name] | |
return sum(grades) / len(grades) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
book = SimpleGradebook() | |
book.add_student('Isaac Newton') | |
book.report_grade('Isaac Newton', 90) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BySubjectGradebook(object): | |
def __init__(self): | |
self._grades = {} | |
def add_student(self, name): | |
self._grades[name] = {} | |
def report_grade(self, name, subject, grade): | |
by_subject = self._grades[name] | |
grade_list = by_subject.setdefault(subject, []) | |
grade_list.append(grade) | |
def average_grade(self, name): | |
by_subject = self._grades[name] | |
total, count = 0,0 | |
for grades in by_subject.values(): | |
total += sum(grades) | |
count += len(grades) | |
return total / count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
book = BySubjectGradebook() | |
book.add_student('Albert Einstein') | |
book.report_grade('Albert Einstein', 'Math', 75) | |
book.report_grade('Albert Einstein', 'Math', 65) | |
book.report_grade('Albert Einstein', 'Gym', 90) | |
book.report_grade('Albert Einstein', 'Gym', 95) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WeigthedGradeBook(object): | |
def __init__(self): | |
self._grades = {} | |
def add_student(self, name): | |
self._grades[name] = {} | |
def report_grade(self, name, subject, score, weight): | |
by_subject = self.grades[name] | |
grade_list = by_subject.setdefault(subject, []) | |
grade_list.append((score, weight)) | |
def average_grade(self, name): | |
by_subject = self._grades[name] | |
score_sum, score_count = 0,0 | |
for subject, scores in by_subject.items(): | |
subject_avg, total_weight = 0,0 | |
for score, weight in scores: | |
# ... | |
return score_sum / score_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
book.report_grade('Albert Einstein', 'Math', 65, 0.2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
Grade = collections.namedtuple('Grade',('score', 'weight')) | |
class Subject(object): | |
def __init__(self): | |
self._grades = [] | |
def report_grade(self, score, weight): | |
self._grades.append(Grade(score, weight)) | |
def average_grade(self): | |
total, total_weight = 0,0 | |
for grade in self._grades: | |
total += grade.score * grade.weight | |
class Student(object): | |
def __init__(self): | |
self._subjects = {} | |
def subject(self, name): | |
if name not in self._subjects: | |
self._subjects[name] = Subject)_ | |
return self._subjects[name] | |
def average_grade(self): | |
total, count = 0,0 | |
for subject in self._subjects.values(): | |
total += subject.average_grade() | |
count += 1 | |
return total / count | |
class Gradebook(object): | |
def __init__(self): | |
self._students = {} | |
def student(self, name): | |
if name not in self._students: | |
self._students[name] = Student() | |
return self._students[name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
book = Gradebook() | |
albert = book.student('Albert Einstein') | |
math = albert.subject('Math') | |
math.report_grade(80, 0.10) | |
# ... | |
print(albert.average_grade()) | |
>>> | |
81.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment