You may work with one partner on this lab.
Run update21 to create the cs21/labs/12 directory. Then cd into your cs21/labs/12 directory and create the python programs for lab 12 here. you will have a file named cd.db that contains an example database of CD information, and a file called cdlinked.py where you will write your solution.
Read through the entire lab before you begin coding. Then go
to the section on Getting Started and follow the instructions
there to begin.
For this lab you will write a program that reads in information from a CD data base into a linked list and then enters a loop with the following menu options for performing different operations on the list:
The cd.db file is formated such that associated with each CD are three pieces of information: artist name, album title, and release year. The information for each CD is stored on a single line and is separated by commas. For example, the the first three lines of the file are:
Public Enemy,It Takes a Nation of Millions to Hold Us Back,1988 James Brown,Get on the Good Foot,1972 Sly & the Family Stone,Fresh,1973
Edit the file called cdlinked.py. In this file, you will create two classes: CDInfoNode and CDLinkedList.
Once you have this class written, add a main function, and test the class by creating some CDInfoNode objects and calling their method functions.
Your CDInfoNode class will form the basis of a CDLinkedList class that will be very similar to the LinkedList class described during class. Follow these steps to get your class started:
Once you have tested your CDLinkedList, use it to create a CD Database by implementing the following:
Once all of the functionality described above is working correctly add an insertSortedByYear method that takes an artist, title, and year as parameters. It creates a new CDInfoNode, and adds it to the CDLinkedList so that the list is sorted in chronological order by year.
Here is some pseudocode to help you get started on this method:
if list is empty or year <= head's year insert new data at head elif year >= tail's year insert new data at tail else set previous to None set current to head while current's year < year set previous to current set current to current's next insert the new node between previous and current in every case need to increment the size field
Modify your function that reads in the data from a file so that it
uses this new method insertSortedByYear rather than the
original method insertAtHead. Once you make this change, and
view all of the data, you should see that it is now in sorted order.
$ python cdlinked.py ############## CD DB Menu ######################## 1. Print all CDs by a given artist and the total number 2. Print all CDs with a given title and the total number 3. Print all CDs released in a given year and the total number 4. Print all CDs in the DB and the total number 5. Quit ###################################################### Enter a value between 1 and 5 : hello Hey, hello isn't a number...try again Enter a value between 1 and 5 : 12 Hey, 12 isn't between 1 and 5 ...try again Enter a value between 1 and 5 : 1 Enter an artist name : De La Soul Matching Albums --------------- 3 Feet High and Rising - De La Soul (1989) De La Soul Is Dead - De La Soul (1991) There are 2 total matches ############## CD DB Menu ######################## 1. Print all CDs by a given artist and the total number 2. Print all CDs with a given title and the total number 3. Print all CDs released on a give year and the total number 4. Print all CDs in the DB and the total number 5. Quit ###################################################### Enter a value between 1 and 5 : 1 Enter an artist name : Lady Gaga Sorry, no matching Albums found for this artist. Perhaps you should buy some. ############## CD DB Menu ######################## 1. Print all CDs by a given artist and the total number 2. Print all CDs with a given title and the total number 3. Print all CDs released on a give year and the total number 4. Print all CDs in the DB and the total number 5. Quit ###################################################### Enter a value between 1 and 5 : 4 The CD DB: --------- Mingus Ah Um - Charles Mingus (1959) Avalon Blues - Mississippi John Hurt (1962) Time Out - Dave Brubeck Quartet (1964) ... SOME RESULTS OMITTED HERE ... One Beat - Sleater-Kinney (2002) Greatest Hits - Al Green (2005) White Blood Cells - The White Stripes (2005) There are a total of 38 CDs in the library ############## CD DB Menu ######################## 1. Print all CDs by a given artist and the total number 2. Print all CDs with a given title and the total number 3. Print all CDs released on a give year and the total number 4. Print all CDs in the DB and the total number 5. Quit ###################################################### Enter a value between 1 and 5 : 5 bye bye
Do not attempt to solve these problems until the required portion of the lab is complete. There are many extensions you could add to this program. Here are a few we thought might be interesting.
Enter the Artist Name : Soul Coughing Enter the Album Name : Ruby Vroom Enter the Year : 1994 Added Soul Coughing, Ruby Vroom, 1994You should do some error checking on the values entered by the user so that you do not corrupt your data base with bad (or incomplete) data.
savefile = open("cdsaved.db", 'w') savefile.write("a, b, c\n") savefile.write("1, 2, 3\n") savefile.close()Add a menu option to allow the user to save the updated database to a specified file. You should write your output file in the same format as the original cd.db file so you could use your saved file as input later.
from sys import argv """ argv is a list of strings containing all the words that appear after the python command """ if len(argv) < 2: #argv[0] is always the name of the file you are running print "usage: python %s <filename>" % (argv[0]) else: #other options start at argv[1] print "reading from file: %s" % (argv[1])
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.