In this project, you will experiment with building your own Python classes. This project is a first-part of a two-part work, where the main goal is for you to build classes to represent a text-version of Facebook.
This is an individual project. Please remember to write down all individuals and sources that you discuss this project with. If you use a different textbook or other written resources, please include the textbook's author/title and URLs.
sort.Implement the Comment class (notice the capital C) in a new file called comment.py (notice the all-lowercase) such that:
Each comment consists of the name of the commentor (as a string object) and the content (as a string object).
The Comment class provides accessor and mutator methods for all instance variables.
You decide the names of your methods, but you must pick descriptive names.
Implement the _ _str_ _ method of the Comment class returns a string that shows the name and the content separated by a colon. For example, Inigo Montoya's comment "I do not think it means what you think it means." should return a string shown below.
>>> from comment import Comment
>>> c1 = Comment("Inigo Montoya", "I do not think it means what you think it means.")
>>> str(c1)
'Inigo Montoya: I do not think it means what you think it means.'
>>> print(c1)
Inigo Montoya: I do not think it means what you think it means.
Write the contract and implementation of the _ _len_ _ method of the Comment class returns the number of characters in the content of the comment (a non-negative integer object).
>>> from comment import *
>>> c2 = Comment("They Might be Giants", 'Science Is Real')
>>> len(c2)
15
>>> Comment('TMBG', 'From the Big Bang to DNA').__len__()
24
>>> c3 = Comment('TMBG', "--it's more like a question that's been put through a lot of tests")
>>> len(c3)
66
Note: This feature is called overloading.
You have overloaded the "str" and "len" functions to have them return information that depends on the context in which they are used.
Many object-oriented programming languages support overloading.
Write the contract and implementation of the _ _contains_ _ method of the Comment class
which takes in an input string and
returns the boolean True if the input string is contained in the content of the content.
>>> from comment import Comment
>>> c4 = Comment("Eric Idle", "Always look on the bright side of life.")
>>> c4.__contains__("bright side of life")
True
>>> "bright side of life" in c4
True
>>> "bright side of life" not in c4
False
>>> "Idle" in c4
False
>>> "Idle" not in c4
True
Note: This feature is called overloading.
You have overloaded the "in" and "not in" symbols to give it a meaning that depends on the context in which it is used.
_ _getitem_ _ method of the Comment class
which takes in an input integer k and
returns the character in the kth position of the comment content of self.
>>> c4 = Comment("Eric Idle", "Always look on the bright side of life.")
>>> c4[0]
'A'
>>> c4[-2]
'e'
>>> c4[0:6]
'Always'
>>> c4[:-1]
'Always look on the bright side of life'
import command
from comment import *
or
from comment import Comment
Use this file to test your implementation of the Comment class against multiple examples.
For example, your file may look like the following
test Python file.
You may submit your test file if you wish, but this is meant for your own use only.
_ _str_ _ method in your submitted file because the grader will test your _ _str_ _ method against the original instruction for Comment class. If you wish to change it, write your work in a separate file).
str, len, +, []).
You may implement the _ _eq_ _ method for Comment class, so that you can use the ==, != comparison operators.
Comment class behave like a list in that it allows
an index assignment.
>>> c4 = Comment("Eric Idle", "Always look on the bright side of life.")
>>> c4[0]="O"
>>> print(c4)
Eric Idle: Olways look on the bright side of life.
>>> c4[1]="o"
>>> print(c4)
Eric Idle: Ooways look on the bright side of life.
>>> c4[-1]="!"
>>> print(c4)
Eric Idle: Ooways look on the bright side of life!
>>> c4[-2]="E"
>>> print(c4)
Eric Idle: Ooways look on the bright side of lifE!
To achieve this, you may add a method _ _setitem_ _ which takes in an integer k and
a string ch (a character), and replaces the kth character of the comment with ch.
_ _iter_ _ in the Python doc here
https://docs.python.org/3/tutorial/classes.html#iterators and see more examples here:
http://www.diveintopython3.net/iterators.html.
You can also search the web for more information and examples.
Implement the Status (notice the capital S) class in a new file called status.py (notice the all-lowercase) such that:
Each Status object consists of the name of the person who posted the status, the status message, names of those who liked the status, and comment objects associated with the status.
The name of poster and the status message must both be strings.
The names of those who have liked the status should be stored as a list of strings (always sorted in alphabetical order). For simplicity, we assume that everyone has a unique name.
The comments associated with the status should be stored as a list of Comment objects. Note the emphasis on Comment objects in the previous sentence.
Status object is created, it does not have any likes or comments.
The constructor method only takes the name of the person who posted the status and the status message.
The following behavior is expected (after you implement the _ _str_ _ method):
>>> from status import *
>>> s1 = Status('Ben Franklin', 'signed the declaration of independence today. Booyah, England!')
>>> print(s1)
Ben Franklin signed the declaration of independence today. Booyah, England!
>>> str(s1)
'Ben Franklin signed the declaration of independence today. Booyah, England!'
Status class provides an accessor method called getLikes which returns the list of names who currently like the status.
Status class provides accessor methods for all instance variables.
You decide the names of your methods, but you must pick descriptive names.
Status class also provides provides mutator methods (you are free to name these methods) for the instance variable corresponding to the poster name and the message.
In addition, the Status class provides the following two mutator methods to change the likers and comments associated to the status:
A method toggleLike which takes a name (as a string object).
If this name is currently not liking the status,
toggleLike adds the name to those who like the status.
Otherwise, toggleLike removes the name from those who like the status.
When a new name is added to the list of likers, this list should be sorted. You can use the .sort list method.
>>> s1.toggleLike('France')
>>> print(s1)
Ben Franklin signed the declaration of independence today. Booyah, England!
France likes this.
>>> str(s1)
'Ben Franklin signed the declaration of independence today. Booyah, England!\nFrance likes this.'
>>> s1.toggleLike('Ben Franklin')
>>> str(s1)
'Ben Franklin signed the declaration of independence today. Booyah, England!\nBen Franklin and France like this.'
>>> s1.toggleLike('Alexander Hamilton')
>>> print(s1)
Ben Franklin signed the declaration of independence today. Booyah, England!
Alexander Hamilton, Ben Franklin, and France like this.
>>> s1.toggleLike('France')
>>> print(s1)
Ben Franklin signed the declaration of independence today. Booyah, England!
Alexander Hamilton and Ben Franklin like this.
A method addComment which takes a Comment object and adds the given comment.
>>> from status import Status
>>> from comment import Comment
>>> s2 = Status('Bob', 'is working on project 7')
>>> newC = Comment('Alice', 'Poor you... Have a cookie!')
>>> s2.addComment(newC)
>>> print(s2)
Bob is working on project 7
Alice: Poor you... Have a cookie!
>>> newC2 = Comment('Bob', "It's all right. The project is a lot of fun! #turningCoffeeIntoCode")
>>> s2.addComment(newC2)
>>> s2.toggleLike('Alice')
>>> print(s2)
Bob is working on project 7
Alice likes this.
Alice: Poor you... Have a cookie!
Bob: It's all right. The project is a lot of fun! #turningCoffeeIntoCode
The _ _str_ _ method of the Status class returns a string with the poster's name, the status message, the names of those who like the status and commments afterwards.
The string should not have a newline character (\n) at the end.
Hint: the string method .format would make your code look neater.
The following string is returned by a Status object whose poster is Ben Franklin and status message is 'signed the declaration of independence today. Booyah, England!'. France liked this Status object, and there are four Comment objects for the status.
'Ben Franklin signed the declaration of independence today. Booyah, England!\nFrance likes this.\nJohn Adams: Bring it, bra.\nBen Franklin: You redcoats don't stand a chance.\nJohn Hancock: Does this mean we get to sign more? Cuz that was fun.\nBen Franklin: So we gathered.'
The output below shows when the above string is printed.
Ben Franklin signed the declaration of independence today. Booyah, England! France likes this. John Adams: Bring it, bra. Ben Franklin: You redcoats don't stand a chance. John Hancock: Does this mean we get to sign more? Cuz that was fun. Ben Franklin: So we gathered.
Note that, if multiple users liked the status, they should be comma separated and the printed line should be grammatically correct. For example, "France likes this." and "Bob and France like this." and "Bob, Daniel, and France like this." Furthermore, the names should appear in alphabetical order.
import command
from status import *
or
from status import Status
Again, use your test file to test your implementation of the Status class against multiple examples.
For example, your file may look like the following
test Python file.
You may submit your test file if you wish, but this is meant for your own use only.
Status class called removeComment. Because we store the comments associated to a status as Comment objects, we first need to add an _ _eq_ _ method to our Comment class.
Status class so that someone can show a reaction (other than 'Like') to a Status object.To do this, you may want to add an instance variable that is a dictionary. The keys are people's names (as strings) and the values are different reactions that people may show. For example, you can steal some ideas from the social networking web application called Facebook (some reactions are Like, Love, Sad, etc).
Add a method for the Status class (for example, you can call it toggleReaction) which takes in a name (as a string) and an allowed reaction
(the data type is up to you) and either add or remove a reaction by that name.
Warning: Make sure that behaviors of your original _ _str_ _, getLikes, toggleLike methods do not change, since the grader will test those against the expected behaviors of the original instructions.
If you wish to change the behaviors of these methods, write them in a separate file.
Submit your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following files:
Comment class.Status class.Be sure that your module can run on IDLE on the computer lab machines (that is, IDLE 3.54).
If you submit your project early, you will get an early feedback on the accuracy of your work.
The grader will test your program against various input values. Check your work against the grading rubric project7gradesheet.pdf which the grader will use when grading your lab.