,
In this project, you will experiment with building your own Python classes. This project is the second half of the Facebook activity, where the main goal is for you to build classes to represent a text-version of Facebook.
If you have submitted a working version of part A of the individual Facebook activity (project 7), you are welcome to work on this project with a partner. Otherwise, please finish project 7 first and find a partner who is in a similar situation.
Note: 90% of the work must be done as a pair-programming activity (as opposed to dividing up parts of the project).
You must not put the name of a partner on the project if the partner has not done at least 90% of the work.
Please tell the lab instructor your partner's name (or whether you are working alone).
If you are submitting a pair project, please only submit one copy of the code with both names and email addresses written in each of the Python files as a comment at the top of the files.
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.
Here is an example of how all the classes come together to make our Facebook work.
>>> TextFacebook = Facebook() >>> TextFacebook.registerUser('Bob', 'bob@gmail.com') >>> TextFacebook.registerUser('Alice', 'alice@gac.edu') >>> TextFacebook.registerUser('Charles', 'charlie@hotmail.com') >>> TextFacebook.login('Bob') >>> TextFacebook.addFriend('Alice') >>> TextFacebook.postStatus('is working on Project 10!') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! >>> TextFacebook.logout() >>> TextFacebook.login('Charles') >>> TextFacebook.addFriend('Alice') >>> TextFacebook.postStatus('is going to watch Star Trek Into Darkness.') >>> TextFacebook.viewStatus() (1) Charles is going to watch Star Trek Into Darkness. >>> TextFacebook.logout() >>> TextFacebook.login('Alice') >>> TextFacebook.viewProfile() Alice (alice@gac.edu) Friends: Bob and Charles >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! (1) Charles is going to watch Star Trek Into Darkness. >>> TextFacebook.toggleLikeStatus(1) >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. >>> TextFacebook.commentOnStatus(0, 'Poor you... Have a cookie!') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! Alice: Poor you... Have a cookie! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. >>> TextFacebook.logout() >>> TextFacebook.login('Bob') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! Alice: Poor you... Have a cookie! >>> TextFacebook.commentOnStatus(0, "It's all right. The project is a lot of fun!") >>> TextFacebook.postStatus("finished working on Project 10! Woohoo!") >>> TextFacebook.logout() >>> TextFacebook.login('Alice') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! Alice: Poor you... Have a cookie! Bob: It's all right. The project is a lot of fun! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. (2) Bob finished working on Project 10! Woohoo!
Put a copy of the comment.py
(notice the all-lowercase) file
from Project 7 in your working directory.
Put a copy of the status.py
(notice the all-lowercase) file
from Project 7 in your working directory.
Implement the User
class (notice the capital 'U') in user.py
(notice the all-lowercase)such that:
Each user consists of his/her name, e-mail address and list of friends (User
objects). When a User
object is created, it does not need any list of friends - only the name and e-mail address are required to create a User
object.
>>> A = User("Jim Halpert","jim@office.com")
The User
class provides accessor and mutator methods for all instance variables.
You decide the names of your methods, but you must pick descriptive names.
User
class provides a method addFriend
which takes a User
object and makes both users friends of each other.
User
class provides a method isFriend
which takes a User
object and returns True
if the given user is a friend and False
otherwise.>>> JH = User("Jim Halpert", "jim@office.com") >>> PB = User('Pam Beesley', 'pam@office.com') >>> JH.addFriend(PB) >>> JH.isFriend(PB) True >>> PB.isFriend(JH) True >>> user1 = User('Ana', 'ana@gac.edu') >>> JH.isFriend(user1) False >>> user1.isFriend(JH) False
Recall that Python lets us define a str function for our own classes by overloading a special method called _ _str_ _
.
The __str__
method of the User
class should return a string containg the name, e-mail address and the names of all friends (in alphabetical order).
Hint: the string method .format
would make your code look neater.
For example, if you print a user whose name is Jim Halpert, whose e-mail address is jim@office.com and whose only friend is Pam Beesley, the string should look as shown below.
'Jim Halpert (jim@office.com)\nFriends: Pam Beesley'
The output below shows when the above string is printed.
Jim Halpert (jim@office.com) Friends: Pam Beesley
If one has no friends, the output should be as shown below.
Chazz Michael Michaels (lonewolf@icerink) Chazz Michael Michaels currently has no friends
If one has more than one friend, the names of all the friends should belong to the same line as shown below.
Bob (bob@gmail.com) Friends: Alice, Charles, and Daniel Alice (alice@gmail.com) Friends: Bob and CharlesMore examples:
>>> print(user1) Anna L (annaL@gac.edu) Anna L currently has no friends >>> str(JH) 'Jim Halpert (jim@office.com)\nFriends: Pam Beesley' >>> JH.addFriend(user1) >>> JH.__str__() 'Jim Halpert (jim@office.com)\nFriends: Anna L and Pam Beesley' >>> User('Al', 'al@gac.edu').addFriend(JH) >>> print(JH) Jim Halpert (jim@office.com) Friends: Al, Anna L, and Pam Beesley
Remember not to print
anything in the _ _str_ _
method. You should accumulate ONE string that contains all the information and return it.
Again, the string .format
method would be useful here.
from user import User
.
type
and isinstance
. These functions are used to check the data type of an object.
Try the following.
>>> from user import User >>> Bob = User('Bob K', 'bobk@gac.edu') >>> type(Bob) # Please find out the outcome yourself >>> isinstance(Bob, User) True >>> isinstance(Bob, str) False >>> isinstance('I am Bob', str) TrueThis may be a useful thing to write on your cheat sheet for the next test.
project8test.py
. Your test should cover all the methods of your classes and various input scenarios. This should include examples similar to the ones given in this page and more.
unFriend
for the User
class which allows the user to no longer be friends with another user. This should make boths users be no longer friends. It should take in a User
object.
User
class which keeps track of friends in a restricted list (as a User
object). Implement a method called setRestricted, setNonRestricted
for the Facebook
class which takes in a name of a person the logged-in person (say, A) is already friends with, and put that user (say, B) in the restricted list.
The next time B is logged in, an error message will show up if B tries to like or comment on a status posted by A.
Implement the Facebook
class (notice the capital 'F') in facebook.py
file (notice the all-lowercase) such that:
The facebook.py
file starts with the following import
commands:
from comment import Comment from status import Status from user import User
Facebook keeps track of all the users and their statuses and the currently logged-in user. Facebook
object also keeps track of unique IDs for statuses. For the purpose of this project, only one user is allowed to use the Facebook at a time. When a Facebook object is created, it does not have any users or statuses.
Use a dict-of-string:user
, where the keys are names and the values are user objects, to keep track of users in the Facebook class.
Use a list-of-status
, where the positions in the list are status IDs and the values are status objects, to keep track of statuses in the Facebook class.
Use a string
(the name of the current user) to keep track of who's currently using your Facebook.
You do NOT have to implement any accessors and mutators for the instance variables in the Facebook
class.
You do NOT have to implement the __str__
method in the Facebook
class.
For examples of outputs from the following methods, refer the the example at the top of this page. None of the following methods returns any value; each method below only prints. Note: you must take advantage of the printing capability which you have implemented for the other three classes.
The Facebook
class provides a method called registerUser
which takes a name and an e-mail address. This method adds a new user to Facebook. It should print an error message if there is already a user with the same name.
The Facebook
class provides a method called login
which takes a name. This method allows a person to log into Facebook with the user associated with the given name. If another user is logged in already, it should print out an error message saying that the other user needs to log off first.
The Facebook
class provides a method called logout
. This method allows the currently logged-in user to log out from Facebook. It should print out an error message if there is no user logged in at the time.
The Facebook
class provides a method called addFriend
that takes a name (a string
). This method allows the currently logged-in user to befriend the user with the given name. This should make both users friends of each other. It should print out an error message if there is no user logged in at the time; or if there is no other user with the given name.
The Facebook
class provides a method called viewProfile
. This method allows the currently logged-in user to view his/her profile that includes his/her name and email address along with the list of his/her friends.
In particular, it should print the user object associated to the person currently logged in.
This method should look a lot simpler than your implementation of the
_ _str_ _
method of your User
class.
The Facebook
class provides a method called postStatus
. This method allows the currently logged-in user to post a new status. Facebook should add this new status. It should print out an error message if there is no user logged in at the time.
The Facebook
class provides a method called viewStatus
. This method allows the currently logged-in user to view statuses of the user and his/her friends. If there is no user logged in at the time, it should not print any statuses and print out an error message saying that a user need to be logged in. The selected statuses should be printed in order of status ID number.
The Facebook
class provides a method called toggleLikeStatus
which takes a status ID number. This method allows the currently logged-in user to like/unlike their own status or a friend's status. If there is no user logged in at the time, it should print out an error message. If the status the user tries to "like" is not his/hers or his/her friend's, it should also print out an error message.
The Facebook
class provides a method called commentOnStatus
which takes a status identification number and a comment (string). This method allows the currently logged-in user to comment on his/her status or his/her friend's status. If there is no user logged in at the time, it should print out an error message. If the status the user tries to "comment" is not his/hers or his/her friend's, it should also print out an error message.
from facebook import Facebook
.
type
and isinstance
. These functions are used to check the data type of an object.
Try the following.
>>> MyFacebook = Facebook() >>> type(MyFacebook) # Please find out the outcome yourself >>> isinstance(MyFacebook, Facebook) True >>> isinstance(MyFacebook, str) False >>> isinstance(MyFacebook, dict) False >>> isinstance(MyFacebook, User) FalseThis may be a useful thing to write on your cheat sheet for the next test.
project8test.py
. Your test should cover all the methods of your classes and various input scenarios. This should include examples similar to the ones given in this page and more.
unFriend
for the Facebook
class which allows the currently logged-in user to unfriend another user. This should make boths users be no longer friends. It should take in a string.
User
class which keeps track of friends in a restricted list (as a User
object). Implement a method called setRestricted, setNonRestricted
for the Facebook
class which takes in a name of a person (say, Bob) which the logged-in person (say, Al) is already friends with, and put Bob in the restricted list of Al.
The next time Bob is logged in, an error message will show up if Bob tries to like or comment on a status posted by Al.
Alternatively, you can make it so that, whenever Bob is logged-in, he cannot see any status posted by Al.
User
class which
allows a user to set their settings to be more private.
If another user (with no mutual friends) tries to add a user with the more private settings, an error message should appear.
Students in past Intro CS 1 classes learned about class inheritance. However, in order to fit in other topics, we decided to stop covering it. If you wish to learn about class inheritance, read the textbook Chapter 12: pages 393-421. You can also look up only python3 object-oriented programming tutorials which discuss inheritance, such as this one: python3_inheritance. This concept would be especially useful if you plan to take Intro CS 2 or other CS classes.
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 from Project 7.Status
class from Project 7.User
class.Facebook
class.project8test.py
, which you have used to test your implementations. Your test should cover all the methods of your classes and various input scenarios.
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 of your work.
The grader will test your program against various input values. Check your work against the grading rubric project8gradesheet.pdf which the grader will use when grading your lab.