After the lab, you should be proficient at
As usual, create a directory for the programs and output you develop in this lab.
Copy the files from your lab10
directory so that you can extend your FaceSpace program in this lab.
You've been writing code for 11 weeks (or so), and the programs have slowly gotten larger. Last week's lab was the largest yet.
lab11
directory in the
terminal, execute the command:
wc ../lab10/*.py
wc
stands for "Word Count". We
called the "word count" command on all the *.py files (i.e., the
Python scripts, not other files or directories) in
your lab10
directory. The first
column of output is the number of lines, the second is the
number of words, and the third is the number of characters in
each file. The total for all the files is listed in the last
line of output.
For example:
161 572 4855 ../lab10/facespace.py 122 289 2828 ../lab10/person.py 265 773 7933 ../lab10/social.py 548 1634 15616 total
wc
command so that you
only see the number of lines, using the command-line argument
"-l
", i.e.,
wc -l ../lab10/*.py
>
operator, i.e.,
wc -l ../lab10/*.py >
mywc.txt
mywc.txt
file, using the Unix commands
more
or cat
or opening the file in a text
editor, such as jEdit. We will come back to this file
later.If you haven't completed the Lab 10 social network program and you
don't expect to finish it, you can use my solution, which is in the
/home/courses/cs111/handouts/lab11/solution/
You
can also compare your solution to mine.
key
function to Person module (10 pts)Create a function that you will later use to sort a list
of Person
objects by their network--lowercased and without
spaces. We saw an example of creating and using a key function in
the Card
class on Friday.
As always, test your function.
As discussed in class, you will update
your SocialNetwork
class to include a method that finds
all the people that belong to some network. The method will take as
a parameter the network name to search for and returns a list
of people in that network. (Note, the method does not take
as a parameter the list of Person objects. Where should that come
from?) Your method should:
Person
's network? Do you have
anything that you can reuse to simplify/reduce your code?
Person
s that belong to that
network.What are good test cases for this method?
Your user interface will be responsible for printing out the people that belong to the network or printing out a message if no people match.
There are new Hollywood-related and other data files
in /home/courses/cs111/handouts/lab11/data_files
Recommended Development Process:
By now, you should know how to do this yourself--we've been breaking down larger problems into small problems all term.
SocialNetwork
class (specifically, in the methods
that you implemented: exporting the people file, reading the people
file, and reading a connections file), add an
appropriate try/except
statement to handle file errors.
Instead of exiting the program, the exception handler should print a
helpful message and return from the method.
A better way to handle exceptions is for the methods to return True iff the reading or writing of a file is successful. The UI would then check what the method returned and display an appropriate message.
There are tradeoffs to putting the exception handling in the social network class vs in the user interface, but we won't talk about them until CSCI209.
Example output:
Which option do you want? p What is the name of the file of people you want to add? s *** Error reading file, s *** [Errno 2] No such file or directory: 's'
Add an option that allows the user to find all the people that belong to some network. This shouldn't be too difficult if you've organized your code well.
Example Run:
ID Name Network Num Friends ------------------------------------------------------------ 1 John Doe W & L 3 2 Jane Smith W & L 2 3 James Jones W & L 2 4 Henry VIII Royalty 2 5 Lady MacBeth Scotland 1 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 (S)earch for people in a network (Q)uit the program Which option do you want? s What network do you want to find that people belong to? scotLAND 1 belong to scotLAND 5: Lady MacBeth in Scotland has 1 friends ... Which option do you want? s What network do you want to find that people belong to? w&l 3 belong to w&l 1: John Doe in W & L has 3 friends 3: James Jones in W & L has 2 friends 2: Jane Smith in W & L has 2 friends ... Which option do you want? s What network do you want to find that people belong to? scot No people belong to scot
Demonstrate that your program works in IDLE and save that in an appropriately named ".out" file. Show at least one example of attempting to read a file that doesn't exist.
Run the wc
command again. This time, view the
size of the .py files in your lab11 directory. Add the output from this
command to your mywc.txt
file using the
>>
operator. An example of running wc
from your lab11 directory is wc -l *.py >>
mywc.txt
Now, when you view your mywc.txt
file,
you should be able to compute how many more lines of code you added to
your files for this part of the project. Document this difference in
comments in your driver program.
For each of the following problems, create a new Python script.
After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab11.x.out, where x is the problem number.
[ [0,1,2], [1,3,5], [2,5,8] ]
Simply print the list; do not worry about formatting yet.
To make each row, you can either use the range
function or append
to a list, but do
not create the list by typing twod=[ [0,1,2],
[1,3,5], [2,5,8] ]
0 1 2 1 3 5 2 5 8
connectfour.py
, csplot.py
,
and games.py
from /home/courses/cs111/handouts/lab11
.
games.py
and look at
the ConnectFour
class. Familiarize yourself with its
attributes and how other methods we discussed are implemented.makeMove
method and its comment
and implement the method. Adhere to the method's comment
description when implementing the method.play
method, as
described in the code. Run connectfour.py
(not
games.py) to test your game.You should only have to modify games.py
and
none of the other files.
You have to hit Control-C in the terminal to kill the game. Attempting to close the window does not work.
You won't submit any output from this program.
In groups of one to three people, implement
the _isDraw
(up to 5 pts) or_isWon
methods
(up to 10 pts) of the ConnectFour class. You can receive partial
credit.
Alternatively, you can improve the _computerMakeMove
method that chooses the "best" column to place the checker (up to 10
pts). A naive first approach (which is currently implemented) is to
randomly pick a column. A more sophisticated approach is to assign a
score to each column that says how "good" a column is for you and
select the column with the highest score. What properties would make
the column's score increase?
If you work in a group, indicate all group members in comments in the code.
turnin
directory. (Review
the UNIX handout if you don't
remember how to do that.)csplot.py
and connectfour.py
into the parent directory. (These are large files that you
don't want to print out.) printLab.sh
command to
create a file to print out. You should probably print from
the labs
directory. You may get a
warning about the directory containing subdirectories. As
long as you don't get an
enscript
error about unknown special
escape, you should be okay.gv
command, such as gv lab11.ps
Print the file using the lpr
command introduced in the first lab.
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!
mywc.txt
file
submitted, comments in driver program)