Lab 10: Designing and Implementing a Music Manager

FAQ

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

Linux

As usual, create a directory for the programs and output you develop in this lab.

You will want to copy the files from /home/courses/cs111/handouts/lab10.

Objective: Programming in Python

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

Problem: Create MyTunes, a program to manage your music collection. Your music collection is a set of songs. You will keep track of songs and allow users to add to their music collection.

Summary of Classes

You can give these classes different names, as long as they make sense.

Organization: The classes should be in one module. The driver program will import the music classes.

The following summarizes the classes and their data and functionality. The longer descriptions for some methods are after the summaries.

PlayTime (given)

Review the PlayTime class inside of music.py. This class represents the length of a song or a music collection. You'll need to use this class in your other classes. You can rename it and add more methods if you want.

Data:

Functionalilty:

Song (25)

Data: Functionality:

MusicCollection (35)

Data: Functionality

Read library from a file

You should have a file called "mytunes.library", which contains your music library, in the "libraries" directory. The format of the file is

number_of_songs
artist1_name
album1_name
song1_name
song1_minutes:seconds
artist2_name
album2_name
song2_name
song2_minutes:seconds
...

Note that this format requires reading files differently from how we have typically read files (for each line...). How will you read in files with this format?

Example collection files can be found in libraries and the handouts directory for lab10. Feel free to share your library files with the class by emailing me with them, and I can post them.

You should use a Song object to maintain information about the song, such as the album, artist, name, and length of the song. You will add each song to the MusicLibrary's list of songs.

By default, check if the filename is in the "libraries" directory, i.e, the filename is "libraries/mytunes.library". Otherwise, add "libraries/" to the beginning of the filename.

minutes and seconds are two-digit numbers that may have a leading 0.

Printing out the Collection

Separate from __str__ method. It's too hard/expensive to accumulate all of this into a string and return it.

Add a method that will print out the music collection, nicely formatted. Include the total number of songs and the length of the music collection.

Example Run:

Title                               Artist               Album                Length  
----------------------------------------------------------------------------------------
Burned                              Better Than Ezra     Before the Robots    00:03:41
Daylight                            Better Than Ezra     Before the Robots    00:03:54
----------------------------------------------------------------------------------------
Total:  2 songs Playtime:  00:07:35

Importing an Album into the Collection

You can import albums from a file into your music collection. The format of the file is

artist_name
album_name
number_of_songs
song1_name
minutes:seconds
song2_name
minutes:seconds
...
The directory albums contains some example album files. Feel free to submit your own albums to share with the class for extra credit. The albums must be in the appropriate format.

Write a method to read in (import) an album. Store the songs in the music collection.

Note: You will append the "albums/" directory to the filename passed into the method (if it isn't already there) so that the user doesn't need to type that everytime.

Example Run:

Enter the name of the file: west.txt

    Reading album from albums/west.txt
    Successfully added 13 songs, totalling 00:51:17

You must print out similar output to above.

Exporting the library to a file

Allow a user to export the library to a file that they specify. The method will take a filename (string) as a parameter. Note that this is the same as the "writing songs to a file" functionality above. (What song method can you use to implement this method easier?)

This method should do the opposite of your method that reads the library in. Whenever you read the number of songs, you write the number of songs.

Example Run (From UI):

What is the name of the file that you want to export the library to? mytunes.saved
        Saving library to file: libraries/mytunes.saved

Note: Your program should add the "libraries/" directory to the filename (if it's not already there) so that the user doesn't have to.

Sorting the Songs in the Collection

Sort the songs in the collection, by title, artist, and then album. (Note that sorting by artist and then album are only to break ties when the title is the same.)

Example Run:

Title                               Artist               Album                Length  
----------------------------------------------------------------------------------------
A Lifetime                          Better Than Ezra     Before the Robots    00:03:29
A Southern Thang                    Better Than Ezra     Before the Robots    00:04:03
American Dream                      Better Than Ezra     Before the Robots    00:03:41
Breathless                          Better Than Ezra     Before the Robots    00:03:36
Burned                              Better Than Ezra     Before the Robots    00:03:41
Burned                              Better Than Ezra     Before the Robots    00:03:43
Daylight                            Better Than Ezra     Before the Robots    00:03:54
Daylight                            Better Than Ezra     Before the Robots    00:03:55
Hollow                              Better Than Ezra     Before the Robots    00:03:31
It's Only Natural                   Better Than Ezra     Before the Robots    00:04:17
Juicy                               Better Than Ezra     Before the Robots    00:03:55
Our Finest Year                     Better Than Ezra     Before the Robots    00:04:23
Our Last Night                      Better Than Ezra     Before the Robots    00:04:13
Overcome                            Better Than Ezra     Before the Robots    00:05:23
Special                             Better Than Ezra     Before the Robots    00:04:05
----------------------------------------------------------------------------------------
Total:  15 songs Playtime:  00:59:49

Testing (15 pts)

Test your classes before moving on to the driver. You'll need to comment out the calls to the test function(s) when you run the driver program.

Best practices: Write the constructor and string representation/print method for each class. Then test them in a test function. Then, write another method and test it. Continue in this way.

Music Manager Driver Program (25)

This program maintains a MusicLibrary object and provides an interface for users to manage their library.

Part 0: Reading in Your Collection (5)

Write your program so that it will allow the user to enter the name of the collection file as a command-line argument. The program will attempt to read that file to initialize the music collection. If you do not give a command-line argument, the program will attempt to read the default "libraries/mytunes.library".

Part 1: The User Menu (15)

Your program will repeatedly prompt the user to select an option and then execute the option if it's valid. If the option isn't valid, your program should reprompt for a valid option.

Your user menu should allow the user to execute each of the following options:

Example user menu:

Select one of the following options:
        (V)iew the library
        (I)mport songs into the library
        (S)ort the library
        E(X)port the library to a file
        (Q)uit the program

Which option do you want? 

Allow the user to enter lower case or capital letters for options. For example, either "V" or "v" is a valid choice for viewing the library.

Note: You could write this with a bunch of if statements. Or, you could create a list of valid options and check if the user's selection is in that list.

Part 2: Storing the Collection Before Quitting (5)

Before exiting the program when the user selects the quit option, you should save the library to the file specified on the command-line.

Style

This is by far the biggest program we have written this semester. Make sure you adhere to good programming practices, such as putting code in functions and using constants, as appropriate. Also, make sure you comment your methods.

Extra Credit

Submitting Album Example (4 pts)

You can submit albums for the rest of the class to use for extra credit. The album must be in the specified format. You can submit up to 3 albums for extra credit, 4 points each.

Finishing up: What to turn in for this lab

  1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
  2. Copy your lab10 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Remove the .pyc files from your directory.
  4. Use the 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.
  5. You can always view the output before you print it, using the gv command, such as
    gv lab10.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!

Grading (100 pts)