Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

First program Give Music would like you to write a program that prints a summary of the albums in a given file. You should write

First program

Give Music would like you to write a program that prints a summary of the albums in a given file. You should write a program that does the following:

also to the given class add a function called extract_all_albums(lines) that reads all the albums that are in the list of lines. The function should return a dictionary that maps the id of an album to the corresponding instance of the Album class. here are all the test codes for this function that needs to be passed

For example:

Test Result
example = [ "Angus's album listing ", " ", "100 ", "Now 1000 ", "Na ", "27.50 ", " ", "# Unbearable albums follow ", " ", "101 ", "Best Birthday Songs ", "Serious Music's ", "10.00 ", " ", "Phew, we're done! " ] albums = extract_all_albums(example) for album_id, album in sorted(albums.items()): print("Album id {}".format(album_id)) print(album) print() 
Album id 100 Album Name: Now 1000 Band: Na Purchase Price: $27.50 Album id 101 Album Name: Best Birthday Songs Band: Serious Music's Purchase Price: $10.00 
file = open('album0.txt') content = file.read() file.close() lines = content.splitlines() albums = extract_all_albums(lines) for album_id in albums: print(albums[album_id]) print()
Album Name: Licensed to Ill Band: Beastie Boys Purchase Price: $25.00 Album Name: Enter the Wu_Tang: 36 Cham... Band: Wu Tang Clan Purchase Price: $25.99 Album Name: Daydream Nation Band: Sonic Youth Purchase Price: $5.00 Album Name: 52nd Street Band: Billy Joel Purchase Price: $25.75
lines = [ "Angus's album listing ", " ", "100 ", "Now 1000 ", "Na ", "27.50 ", " ", "Phew, we're done! " ] albums = extract_all_albums(lines) # Check dictionary value is an Album album_100 = albums[100] print(isinstance(album_100, Album)) 
True

The input sequence of lines will contain zero or more album specifications of the form given in the previous question. There may be other lines between the various album definitions; you must ignore these lines. You may assume all albums are correctly formatted.

  • Prompts the user to enter a file name
    • Use the prompt 'Enter a data file name: '
    • If the user enters an invalid file name then print 'Invalid File Name Entered!' and ask the user to enter a file name using the original prompt.
    • Keep asking until the user has entered a valid file name.
  • Reads the album data into the program as a list of lines.
  • Processes the list of lines to extract all the albums as a dictionary.
  • Prints a table as follows:
    • Print a newline
    • Print a title for the table: Album Catalogue Summary
    • Print a separator of 35 '-' characters
    • Print a line with the column headings 'ID' and 'ALBUM NAME' in the appropriate places (see format string below).
    • Print a row for each album filling the columns, sorted by album ids. Use the format string below to get the columns lined up as required.

Notes:

  • This format string may be of use: '{:<5}{:<30}'.
  • The input data files contain sales sections which should be ignored at this stage.
  • At the start of your program you should include the line import os
  • You can use os.path.isfile(filename) to check if a file name is valid

the previous code:

class Album: """Stores the information about an album""" def __init__(self, album_id, name, band, cost): """The data that forms an album, note that: - album_id is a int - cost is a float - the rest are strings """ self.album_id = album_id self.name = name self.band = band self.cost = cost self.total_sold = 0 def add_to_sold(self, count): """Adds to the internal album counter of the album""" if count >= 0: self.total_sold += count else: template = "Attempt to add {} to Album: {}. VALUE MUST NOT BE NEGATIVE" raise ValueError(template.format(count, self.album_id)) def __str__(self): output = "" output += "Album Name: {} ".format(self.name) output += "Band: {} ".format(self.band) output += "Purchase Price: ${:.2f}".format(self.cost) return output def __repr__(self): return "<<{}>>".format(self.album_id) def extract_album(lines, index): """Insert your function here""" identity = lines[index+1].strip() album = lines[index+2].strip() band = lines[index+3].strip() price = float(lines[index+4].strip()) return Album(identity, album, band, price)

album0.txt

323 Licensed to Ill Beastie Boys 25.0 70 Enter the Wu_Tang: 36 Cham... Wu Tang Clan 25.99 turning to Alice, she went on, `What's your name, child?' and there stood the Queen in front of them, with her arms folded, 115 Daydream Nation Sonic Youth 5.0 414 52nd Street Billy Joel 25.75 2010-06-09,115,8255 2015-03-15,70,6264 2010-08-09,323,684 2012-01-06,323,7340 2009-01-01,414,13164 2017-04-02,70,3259 2015-06-05,323,2378 2015-03-05,115,9463 2010-12-10,414,12952 2013-05-29,323,1687 2013-04-07,115,1594 2015-10-19,70,1469 2011-10-07,414,10867 2006-04-14,115,5806 2014-11-27,70,9864 2006-08-22,323,7088 2017-12-19,115,5970 2012-04-16,414,16813 2007-05-13,414,11626 2006-09-18,70,2641 2006-12-29,323,5578

For example:

Input Result
album0.txt
Enter a data file name: album0.txt Album Catalogue Summary ----------------------------------- ID ALBUM NAME 70 Enter the Wu_Tang: 36 Cham... 115 Daydream Nation 323 Licensed to Ill 414 52nd Street 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

global cybercrime

Answered: 1 week ago

Question

1. Define the goals of persuasive speaking

Answered: 1 week ago