Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extracting all albums from a list of lines Write a function extract_all_albums(lines) that reads all the albums that are in the list of lines. The

Extracting all albums from a list of lines

Write a function 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.

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.

You will need to paste your entire answer from the previous question, including the Album class, extract_album, plus your new extract_all_albums function into the answer box.

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)

test album0:

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:

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

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

Step: 3

blur-text-image

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

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

Discuss the goals of financial management.

Answered: 1 week ago

Question

2. Discuss the steps in preparing a manager to go overseas.

Answered: 1 week ago