Lab 10: Using and Defining Classes

--- FAQ ---

Table of Contents:

Goals

After the lab, you should be proficient at

  1. creating and testing your own classes from a specification
  2. developing a larger program (set of classes) to solve a data management problem

Deadlines Update

We will have a staggered deadline for this lab:

Objective: Set Up

  1. Run the help client

    run labhelp

  2. Instead of creating a lab10 directory, copy the entire /csci/courses/cs111/handouts/lab10 directory and all its contents to your cs111 directory (which again means you should use what command-line option?).

Review

Review the slides for today's lab.

For this lab, you'll use "real" program names instead of the "lab10_x.py" names we typically use.

Objective: Preparing to Create a Social Network

Problem: Create InstaFace, an application to manage social networks. A social network is a set of users, some of whom are friends. You will keep track of the users and allow users to add friends to their social network.

What is provided

Testing (25 pts)

Before you start implementing, we want to make sure that you know that you need to TEST! The points for testing have been separated out explicitly from the other code implementation to show testing's importance.

Test each class in its test function before moving on to the user interface.

Writing tests helps you isolate problems and debug more easily. If you were to find a problem when you use the user interface, you won't know if there is a problem with your class or with the user interface's use of the class.

Best practices:

  1. Implement (i.e., override) the constructor and string representation method for each class.
  2. Programmatically test the string representation.
  3. Implement another method and test it.
  4. Continue in this way. You'll need to adapt, depending on the purpose of the methods.

If you ask for help and have not sufficiently tested the other methods you already wrote, we will not help until after you have tested and (perhaps) debugged other methods. Your programs are becoming larger, more complex, and more difficult to debug if you don't test each part, individually, to make sure that they are all working.

Output

Save the output from executing your tests for each class in an appropropriately named file, e.g., person.out

Test Data

I have provided a number of test files for the social network that should be in the data directory of your lab10 directory.

The files are named such that you'll know which is the "people" file and which is the "connections" file. For example, the smallest social network is created from default.txt, which contains the people, and default_connections.txt, which contains the connections.

(simple.txt and simple_connections.txt are copies of the respective default files, in case the default files are corrupted.)

Organization

Each class should be in its own module. The driver/UI program will import the SocialNetwork class, and the SocialNetwork will import the Person class. Edit the files that you copied during the Set Up step.

Implementing the Classes

Now you're ready to start programming!

Class: Person (20)

This class represents an individual in a social network. You'll need to use this class in your other classes.

Data:

Review data type discussion from class.

Functionality:

Class: Social Network (54)

The provided template includes the method headers, doc strings, and placeholders for the method body. The placeholders should be removed after you've implemented the method. An overview of the methods are described below, with links to more detailed descriptions of the more complex methods.

You should implement a few methods and then start testing. If you wait to test, you will make similar mistakes.

Data:

Functionality:

The more complex methods are described in more detail below:

String Representation

Already implemented, but requires other methods to be implemented. The string you create should represent the SocialNetwork but does not need to look as "pretty" as the display method, described later. In the given implementation, the string representation contains all the Persons in the social network. (You may want to have some order for the Persons so that it's easier to find them.)

Displaying the Social Network

Display the people in the network in a pretty, sorted-by-id way. For example, for the Hollywood social network:

ID        Name                           Num Friends
----------------------------------------------------
0000      Brie Larson                              5
0001      Scarlett Johansson                       5
0002      Henry Cavill                             6
0003      Ben Affleck                              8
0004      Amy Adams                                3
0005      Samuel L. Jackson                        8
0006      Gal Gadot                                8
0007      Chris Hemsworth                          7
0008      Jason Momoa                              5
0009      Ray Fisher                               9

The output above doesn't always render correctly in the browser because "ff" in Affleck gets rendered as one character rather than two.

Adding People to the Network, from a File

See the Lab 10 Slides for the algorithm.

This method will take as input the name of a file and process the file, which contains people that should be added to the social network. (Note that the people do not have friends yet.) To make your code simpler, leverage a method you already wrote.

The format of the file is below:

num_of_users
user_id
name
user_id
name
...

Adding Connections to the Network, from a File

See the Lab 10 Slides for the algorithm.

This method will take as input the name of a file and process the file, which contains the connections between people in the social network. Each line of the file contains two ids and represents the people with those ids are friends.

The format of the file is below:

user_id user_id
user_id user_id
...

Exporting the people to a file

After updating your social network, you may want to save the network to a file so that you can use it later. There are two parts to the network: the people/users and the connections between the people.

This method should write the people in the social network to a file, named by the parameter to this method. The format of the file is as seen above. Essentially, you want to do the opposite of your method that reads in the people from the file.

A good test of this function is to write out the social network (that you did not update) to another file and then check if the files match.

Exporting the connections to a file

This method handles writing the connections to a file. If you're not using the provided template, copy this method from the template. Note the assumptions about the method names. You may need to edit this method to match your API.

Objective: Using the Classes via a User Interface Program - InstaFace (16)

This program maintains a SocialNetwork object and provides an interface for users to manage the social network. If you developed the Person and SocialNetwork classes correctly, the majority of this program should be getting input from the user and calling Person and SocialNetwork methods.

The bulk of the code has been written for you and is in the instaface.py file. If you're having issues with the user interface, make sure that the given program calls the correct methods, as named in your class files.

Part 0: Reading in Your Social Network (3)

Your program should allow the user to enter the names of the people file and the connections file as command-line arguments (see the lab slides). The program should call the appropriate methods to read those files to initialize the social network. If the user does not provide command-line arguments, the program will attempt to read the default files, data/default.txt and data/default_connections.txt.

You must test command-line arguments from the terminal. If you use IDLE, you'll always use the defaults.

For example, in a terminal, navigate to your cs111/lab10 directory and run: python instaface.py data/hollywood.txt data/hollywood_connections.txt

Part 1: Interactions with the User (10)

Most of the interactions with the user are implemented for you. You are responsible for implementing the following interactions:

Viewing a person's information:

Which option do you want? v

What is the id of the person you want to view? doej
doej: Jane Doe has 2 friends
Which option do you want? v

What is the id of the person you want to view? a
There is no person with id a

Adding a user and displaying the updated network:

Which option do you want? u

What is the id of the user to add? doej
That user name is already taken

What is the id of the user to add? ht8
What is the name of the user to add? Henry the Eighth

Henry the Eighth has been added to the network

********************************************************************************
Select one of the following options:
        (D)isplay the social network
        (V)iew a person in the social network
        Add (P)eople to the social network from a file
        Add (C)onnections to the social network from a file
        Add a (U)ser to the social network
        Add a pair of (F)riends to the social network
        E(X)port the social network
        (Q)uit the program
Which option do you want? d

ID         Name                           Num Friends
-----------------------------------------------------
astleyr    Rick Astley                              1
doej       Jane Doe                                 2
ht8        Henry the Eighth                         0
schmoj     Joe Schmo                                1

Part 2: Storing the Social Network Before Quitting (3)

Before exiting the program when the user selects the quit option, you should save the social network to the files specified on the command-line/the default files, as appropriate.

Output from InstaFace

Run InstaFace in Idle (which means that it will not test your use of command-line arguments--I will test that functionality) and save the output, as usual, in an appropriately named file. Make sure you demonstrate the error handling abilities of your code. This is your grand finale! Show me what you got!

Extra Credit

Additional Functionality (up to 12 pts)

We discussed as a class some additional functionality that your classes may have. After you have completed the required functionality, you can add more functionality. (When implementing, consider which class should implement this functionality?) Some of suggestions include

Searching by name will wait until Lab 11.

More points will be awarded for completely implemented functionality (e.g., integrated into Person, SocialNetwork, and instaface) rather than implementing a lot of functionality.

Finishing up: What to turn in for this lab

Save paper: Make sure you don't have any data files (e.g., outputs from the exported people files) in the lab10 directory. Those files should be in your data directory.

    Note that each command links to a page with more information about using the command.

  1. Create the printable lab assignment, using the createPrintableLab command.
  2. View your file using the evince command.
  3. Check that the PDF contains all (and only) the necessary files.
  4. Print the file from evince. You can print to other printers if there are issues with the computer science printers (which do not cost you anything to print computer science work).
  5. Submit your lab directory into your turnin directory.
  6. Log out of your machine when you are done.

Labs are due at the beginning of Friday's class. The electronic version should be in the turnin directory before class on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (115 pts)