Skip to article frontmatterSkip to article content

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 --strict

Group Activity: Ed Posting

Define a class EdPost that represents a question on the discussion board. The EdPost class should have an initializer that takes an argument for each of the 4 fields your class should have. Each field should use the same names as its parameter, except that it needs to be declared private.

  • The bool flag that marks the post as private or public. This field does not have a default value. If True, then the post is public. If False, then the post is private.
  • The str title of the post. This field does not have a default value.
  • The str tag of the post. If the parameter is not given, the tag field should be 'General'.
  • The list[str] comments on the post. If the parameter is not given, the comments field should be [].

Your class should have the following methods.

  • An initializer that takes the parameters to initialize the fields as described above (in that order).
  • A method get_title that returns the title of the post.
  • A method get_tag that returns the tag on the post
  • A method add_comment that adds a comment (str) to this post.
  • A method display that prints out information about the post the following format, replacing the uppercase placeholders for TITLE, TAG, COMMENT, and ... accordingly. The comments should appear in the order they were added, one on each line and indented by two spaces.
TITLE (TAG)
Comments:
  COMMENT
  COMMENT
  COMMENT
  ...

If the post is private, then the display method should only print "Private post!" instead (with no comment or tag information).

Here’s an example call:

post1 = EdPost(True, "Typo in spec?", "Education")
post1.add_comment("There was a typo!")
post1.add_comment("And maybe another typo?")

post2 = EdPost(True, "What's Wen's favorite cat?")
post2.add_comment("There can't be just one!")

post3 = EdPost(False, "I need help!", "Processing")

post1.display()
print()
post2.display()
print()
post3.display()

And its output:

Typo in spec? (Education)
Comments:
  There was a typo!
  And maybe another typo?

What's Wen's favorite cat? (General)
Comments:
  There can't be just one!

Private post!
class EdPost:
    def __init__(...) -> ...:
        ...

    def get_title(...) -> ...:
        ...

    def get_tag(...) -> ...:
        ...
        
    def add_comment(...) -> ...:
        ...

    def display(...) -> ...:
        ...
post1 = EdPost(True, 'Typo in spec?', 'Assignment 1')
post1.add_comment('There was a typo!')
post1.add_comment('And maybe another typo?')

post2 = EdPost(True, "What's Wen's favorite cat?")
post2.add_comment("There can't be just one!")

post3 = EdPost(False, "I need help!", "Processing")
post3.add_comment("TA response")

post1.display()
print()
post2.display()
print()
post3.display()

The following code cell creates a dictionary of EdPost objects, where the keys are the name of the EdPost in the format edpostX, where X is a number. The values are the objects themselves. The passkey for today’s Section Participation assignment in Canvas is the category tag for edpost32. Enter the category without the parentheses.

import pandas as pd

ed_board = pd.read_csv("ed_board.csv").to_dict("records")
ed_posts = {}

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
...

Whole Class 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 author parameter 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 display method 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 display(...) -> ...:
        ...

Now, let’s mess around with authored Ed posts!

post3 = AuthoredEdPost(True, "Who's the best superhero?", "Social")
post3.add_comment("Superman!")
post3.add_comment("Clearly it's Batman", "Vatsal")
post3.add_comment("Any X-men fans?", "Suh Young")

post4 = AuthoredEdPost(True, "Does Kevin like cats?")
post4.add_comment("No comment", "Kevin")

post1.display()
print()
post2.display()