CS 116: Introduction to Computer Science 2

CS 116 Tutorial 10: Reading and Writing Files


Reminder: Assignment 09 is due on Monday Dec. 2nd at 10AM (accept late submission)


class CD: ''' Fields: artist (Str), duration (Nat), songs (listof Str) where * artist is the name of the performer of the album * duration is the length of the CD in seconds * songs contains the names of the songs on the CD. ''' def __init__(self, artist, dur, los): ''' [purpose]: Initializes a CD [fields]: * artist (artist's name) - Str * duration (duration of CD in seconds) - Nat * songs (list of artist's songs) - (listof Str) ''' self.artist = artist self.duration = dur self.songs = los def __repr__(self): ''' [purpose]: Represents a CD [format]: Artist's Name: XXX Duration: XXX Songs: XXX ''' songstr = "" for song in self.songs: songstr += song + ", " songstr = songstr[:-2] return "Artist Name: {0}\nDuration: {1}\nSongs: {2}".format\ (self.artist, self.duration, songstr) def __eq__(self, other): return isinstance(other, CD) \ and self.artist == other.artist \ and self.duration == other.duration \ and self.songs == other.songs
  1. Write the Python function make_cd_dict that reads the data from a text file, fname, and returns a dictionary of CD objects containing the data from the file, with keys equal to the name of the CD.
    { album title: CD(artist, duration, [listof songs]) }

    You may assume that each CD has a unique name.

    The files will be formatted in the following way:

    name of artist, name of CD, length in seconds, song titles
    (separated by a comma and at least one space).


    Example File:

    Yes, Close to the Edge, 2266, Close to the Edge, And You and I, Siberian Khatru
    Various, Disney Mix, 912, You'll Be in My Heart, Kiss the Girl, Circle of Life, I'll Make a Man Out of You, Whole New World, Go the Distance
    Pink Floyd, Wish You Were Here (Side 1), 1268, Shine On You Crazy Diamond (1-5), Welcome to the Machine
    Jonathan Coulton, Code Monkey, 203, Code Monkey
    The Beatles, Abbey Road, 255, Mean Mr. Mustard, Polythene Pam, She Came in Through the Bathroom Window

  2. Write the Python function output_CD_info that consumes a dictionary of CD objects, cd_dict (like the one returned in Q1) and a string, name.
    If the string is not a key in the dictionary, the function should do nothing.
    If the string is a key in the dictionary, the function should create a text file named "Artist - Title".txt, where Artist and Title relate to the cd found at cd_dict[name].
    The text file should contain (each on its own line):

    • the title of the CD in uppercase,
    • then the artist,
    • then the duration of the CD (formatted like in question 1),
    • then a blank line, and
    • then the index of the song with name of each track in the CD.

    d = {"Disney Mix": CD("Various", 912, ["You'll Be in My Heart", "Kiss the Girl",
                                           "Circle of Life", "I'll Make a Man Out of You",
                                           "Whole New World", "Go the Distance"])}

    For example, output_CD_info(d, "Disney Mix") would create a file named "Various - Disney Mix.txt" containing:

    DISNEY MIX
    Various
    912 sec
    
    1. You'll Be in My Heart
    2. Kiss the Girl
    3. Circle of Life
    4. I'll Make a Man Out of You
    5. Whole New World
    6. Go the Distance
    
    

Valid XHTML 1.0 Strict

Last modified on Friday, 20 December 2019, at 14:04.