Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Created November 27, 2015 09:25
Show Gist options
  • Save CleverProgrammer/5726270a0bb542b40459 to your computer and use it in GitHub Desktop.
Save CleverProgrammer/5726270a0bb542b40459 to your computer and use it in GitHub Desktop.
Student class that models a student. Final version.
"""
Author: Rafeh Qazi
Create a student class that models a student.
Date: 11/26/2015
Function_Based_Gist: https://gist.github.com/Rafeh01/4a6d6b79eee4caf6b799
Update: 11/27/2015
Final_Gist:
Key Features:
1. Class
2. Class Object Counter
3. @classmethod Decorators
4. Selection Sort
5. namedtuples
6. Tuple Unpacking
7. Context Manager
8. Methods taking class
9. pep8 Do Not Exceed Line Beauties
10. Solid Documentation
11. String Formatting
12. Used classmethods to improve readibility
13. Emphasis on readability
"""
from collections import namedtuple
class Student:
student_counter = 0
all_students = []
top_student = None
def __init__(self, name, hours, quality_points):
self.name = name
self.hours = float(hours)
self.quality_points = float(quality_points)
self.result = self.quality_points / self.hours
Student.student_counter += 1
Student.all_students.append(self)
Student.top_student = Student.best_student()
@classmethod
def best_student(cls):
"""
Take all_students as input and return the student with the
best GPA.
:return: object
"""
top_student = cls.all_students[0]
for student in cls.all_students:
if student.result > top_student.result:
top_student = student
return top_student
@classmethod
def create_students(cls, filename):
"""
Read file and create student objects.
:param filename: string
"""
Student_info = namedtuple('Student_info',
'last_name first_name hours q_points')
with open(filename, mode='r') as f:
for line in f:
line = Student_info(*line.split())
full_name = str(line.first_name).capitalize() + ' ' + str(line.last_name).capitalize()
cls(full_name, line.hours, line.q_points)
@classmethod
def print_results(cls):
"""
Take the student with the highest GPA and print their report.
Print the report of the best student.
"""
assert isinstance(cls.top_student, cls)
string = cls.top_student.name, cls.top_student.hours, cls.top_student.result
print('The best student is {0} who has spent {1} hours and has a {2} gpa.'.format(*string))
def main():
Student.create_students('students.txt') # creates student objects
Student.print_results()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment