Question: Extracting an album Write a function extract_album(lines, index) that takes a list of lines and an index into that list and returns an instance of

Extracting an album

Write a function extract_album(lines, index) that takes a list of lines and an index into that list and returns an instance of an Album. The album information is in the six lines starting at lines[index], an example list of lines could be:

[ "This is an example list of lines ", "Not all lines in the given list ", "correspond to an album ", " ", "101 " "Now 1000 ", "Misc Artist ", "27.50 ", " ", "Guess this is then end of the file " ]

In this case index should be 3 as the album information starts on that line.

The six lines that form an album are:

  • The opening tag
  • A unique album ID
  • The album name
  • The name of the band
  • The purchase price
  • The closing tag

Notes:

  • The list of lines will usually come from reading a file and therefore you will need to strip any newline characters (ie, ) off the end of each line.
  • The answer box is preloaded with the Album class. You will need to add your extract_album function to the end of it.

here is the code that needs to be edited:

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"""

For example:

Test Result
example = [ " ", "101 ", "Now 1000 ", "Misc Artist ", "27.50 ", " " ] print(extract_album(example, 0))
Album Name: Now 1000 Band: Misc Artist Purchase Price: $27.50 
file = open('album0.txt') content = file.read() file.close() lines = content.splitlines() californication = extract_album(lines, 0) print(californication) # Check returned value is an Album object print(type(californication))
Album Name: Licensed to Ill Band: Beastie Boys Purchase Price: $25.00 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!