For this section, you will be implementing a University class. We have provided an edited version of the Student class from lecture.
In this lesson, we’ll practice type annotations and class-building to create some Ed posts!
%pip install -q nb_mypy
%reload_ext nb_mypy
%nb_mypy mypy-options --strictStudent Class¶
class Student:
'''
The Student class represents a single student. It has a name and
contains functionality to store and give access to a student's
schedule and credit-load.
'''
def __init__(self, name, schedule):
'''
Initializes the Student object with the given name
and schedule file.
'''
self._name = name
for post in ed_board:
is_public = post["is_public"]
title = post["title"]
tag = post["tag"]
comments = post["comments"]
ed_posts[f'edpost{post["id"]}'] = EdPost(is_public, title, tag, comments)
# Task: Find the category for edpost32
...AuthoredEdPost Activity¶
Uh-oh, looks like we forgot a very important feature of Ed posts-- their authors! Let’s make an AuthoredEdPost class to take in a string author parameter (if none is provided, the author should be "Anonymous"). Authors can write Ed posts, but they can also add comments! All methods in the AuthoredEdPost class are the same as those in the EdPost class, with some excpetions. Do the following tasks in the AuthoredEdPost class:
Add the
authorparameter to the initializer. If no author is provided, default to"Anonymous". This parameter should be a string.Add a method
get_author()that returns the author of a post.Add a method
get_commenters()that returns a dictionary where the keys represent the authors of comments and the values are a list of that author’s comments. (Hint: you may need to create a new field to help with this!)Update the
add_comment()method so that you are keeping track of the author of each comment in addition to the comment itself. If there is no author given, default to"Anonymous".Change the
displaymethod so that it aligns with the following format:
TITLE (TAG) by AUTHOR
Comments:
COMMENT_AUTHOR: COMMENT
COMMENT_AUTHOR: COMMENT
COMMENT_AUTHOR: COMMENT
...AUTHOR is the author of the EdPost, while each COMMENT_AUTHOR is the author of their respective comment.
class AuthoredEdPost:
def __init__(...) -> ...:
...
def get_title(...) -> ...:
...
def get_tag(...) -> ...:
...
def get_author(...) -> ...:
...
def get_commenters(...) -> ...:
...
def add_comment(...) -> ...:
...
def get_name(self):
'''
Returns the name of this student.
'''
return self._name
def __getitem__(self, course):
'''
Returns the credit-worth of the given course. If the
student is not enrolled in the course, return None.
'''
if course in self._courses:
return self._courses[course]
else:
return None
def get_courses(self):
'''
Returns a list that contains the names of all the classes
the student is currently enrolled in.
'''
return list(self._courses.keys())
post3 = AuthoredEdPost(True, "Who's the best superhero?", "Social")
post3.add_comment("Superman!")
post3.add_comment("Clearly it's Batman", "Advaith")
post3.add_comment("Any X-men fans?", "Oliver")
post4 = AuthoredEdPost(True, "Does Rukman like cats?")
post4.add_comment("No comment", "Rukman")
* Don't forget that all fields you define in your `__init__` should be private and begin with a `_` !
* Remember that the name of the student is in the file name. This means you might need to extract the name from the file name string. For example, reggie.txt should be transformed into reggie!
### Problem 2: `get_mean_credits`
This method will return the average total credits of all students enrolled in the University. Should return None if no students are enrolled.import os
class University:
'''
A University represents a collection of Students. It has a name and
contains functionality to compute aggregate statistics about its enrolled
students.
'''
def __init__():
'''
Implement an initializer for the class University. It should take in
a string name representing the name of this University and the directory
of schedule text files.
'''
def get_mean_credits():
'''
Returns the mean amount of credits that students are taking.
If there are no students enrolled, return None.
'''
uw = University("University of Washington", "students")
uw.get_mean_credits()