CS 110X Feb 19 2014 : LAB FIVE
If at first you don’t succeed, try, try again...
1 Practice... Practice... Practice
1.1 Lab Preliminaries
In this lab, you will prepare for the final two homeworks in this course. I also want to get you some experience in writing CSV files, which will open up new ideas for you to think about.
1.2 Installing requests and serial packages
To get you ready for HW6 I need you to install the request package in Canopy. You will do this using the lab computers, but I’m also asking you to follow these steps on your personal computer/laptop.
Launch Canopy and click on the Package Manager icon. If you are running the Canopy editor instead, select the Menu Item Tools | Package Manager. You will be told that you cannot connect to the Canopy Server. Do not worry! At this point you need to log in (note the upper right hand corner gives you a link that you can click on to login.
You will need to supply your email address and password that you used when you downloaded canopy for the first time. If you don’t remember your password, do not panic! Simply click on the "Forgot Password" link and you will be sent an email that will allow you to reset your password. If you don’t have an account, create one using your wpi.edu email address.
Assuming that you have logged into the Canopy Package Manager properly, you can search for the required packages I want you to install.
If you cannot get by this step, be sure to seek additional help from the TAs and SAs outside of this lab. This step is not needed for the lab, but is required for HW6 and Lab 6/7.
In the upper left corner of the Package Manager window there is a search field (with a magnifying glass in it). Type in request and choose to install the requests 1.2.3-1 entry that appears.
Do this by selecting it on the right and click on the Install button. If this step doesn’t work, contact the TAs and SAs outside of this lab to see what is wrong.
Assuming this worked, then type in serial into the search field and select the pyserial 2.6-1 entry that appears. Install this package as well.
Note: if you encounter any issues with installing the pyserial package for free, then stop. Just yesterday I was able to install for free, but now I hear reports that they are asking for money. You don’t need pyserial for this lab. Post your experience in trying to install pyserial and any questions you might to the LAB5 discussion forum in my.wpi.edu
You are now ready to progress to the lab assignment.
1.3 Lab Assignment
For this lab you are to write code that allows the user to enter in a series of (key, value) pairs and then store the dictionary in a CSV file on disk.
I have provided you with a template lab5.py file to get started. This file contains three functions:
lab5() – this is the function you execute but do not have to modify.
inputDictionary() – this function returns a dictionary of string literal keys associated with string literal values. YOU MUST MODIFY THIS FUNCTION
storeDictionary() – this function stores a dictionary to a CSV file on disk. YOU MUST MODIFY THIS FUNCTION
If you are opening this file in Canopy, be sure to change the working directory to be "the Editor Directory" as you have seen me do in class and on the homework assignments.
You must change the code to support the following behavior:
>>> lab5() Type the key (or press ENTER when done): test Enter the value to associate with test: this out Type the key (or press ENTER when done): 5 Enter the value to associate with 5: something Type the key (or press ENTER when done): Created file myOutput.csv
At this point if you open up the myOutput.csv file you will see it contains two rows of information:
test this out 5 something
For inspiration on how to deal with user input, review the class lecture that covered how to use raw_input.
1.4 Change inputDictionary
You must change the inputDictionary function to read input from the keyboard using the raw_input function. Note that you will populate the dictionary with string literal keys that are associated with string literal values.
This function must return the dictionary that was populated by the user’s input, conforming to the example above.
1.5 Change storeDictionary
Right now this function simply creates a CSV file with two rows. You need to write code that will extract all keys (and their corresponding values) from the dictionary and writes them to a CSV file with two columns.
Note that the argument to the csv.writerow method is a list.
You can validate that you have a working program by opening the generated CSV file within Excelt to confirm that it contains the values you entered by keyboard.
1.6 Get Credit For This Lab
To receive credit for this lab, you must submit your code solution as the assignment lab05 in the Web-based Turnin System.
Be sure to select assignment lab05 and submit your lab_5.py file as an individual.
1.7 Version : 2014/02/21
(c) 2014, George Heineman