(Solved) : Using Python 37 Part 2 Playlist Class Exercise 3 Exploring Playlist Class Read Playlistpy Q42750836 . . .

USING PYTHON 3.7:

Part 2: The Playlist class

Exercise 3 – Exploring the Playlist class

Read through the Playlist.py class to learn how a playlist isdefined. Notice the attributes that make up a playlist. Inparticular, notice that the duration of a playlist is determined bylengths of all of the songs.

  1. a) The three playlists are printed. On paper, write out what youthink the output should be for the print statements. How is theoutput of a Playlist object determined?

  2. b) Call the necessary Playlist method(s) to update the name ofplaylist2 so that it is now named “Classics”. The following printstatement should now reflect this change.

  3. c) Write tests to determine whether the duration attribute forthe playlists is accurate. How is the value for the durationdetermined?

  4. d) Add a song to a playlist and write tests to determine theplaylist has been updated correctly.

Exercise 4 – Testing if two playlists areequal

Inside assignment9.py, add tests to test_are_equal() tocomprehensively test if two playlists are equal. InsidePlaylist.py, add code to the __eq__ function so that it correctlydetermines if two playlists are equal. For this assignment, assumetwo playlists are equal if they contain all of the same songs(although the songs do not have to be in the same order within thelist).

Exercise 5 – Getting the current status of a playlistafter listening for a given amount of time

Inside assignment9.py, add tests to test_get_status() tocomprehensively test theget_status() method. Inside Playlist.py,add code to the get_status() method so that it works correctly. Theget_status() method takes an integer as a parameter denoting thenumber of seconds a user has been listening to a playlist. Thefunction returns a tuple representing the current song, and howmany seconds the user is into that song.

Given a playlist that contains the following songs: ‘Champions’by Queen (220 secs), ‘Africa’ by Toto (295 secs), and ‘Hey Jude’byThe Beatles (431 secs).

If get_status() is called with 500 as the argument, that meanswe have listened for 500 secs… We would have finished listeningto ‘Champions’ (220 seconds), but not have finished listening to‘Africa’. In fact we would be 280 seconds into the song ‘Africa’,so (“Africa”, 280) would be returned.

If the time given is below 0, or greater than the total durationof the playlist, return a tuple containing the first song in theplaylist, and time 0.

Exercise 6 – Testing if two playlists areequal

Inside assignment9.py, add tests to test_next_song() tocomprehensively test thenext_song() method. Inside Playlist.py, addcode to the next_song() function so that it works correctly. Thenext_song() method takes a string representing the title of a songas a parameter, and returns the song object that comes after thatsong in the playlist. You may assume there is a song in theplaylist with the given title.

If the title given is the title of the last song in theplaylist, then the first song is returned.

PHYTON CODES:

tests = 0
passed = 0
import Playlist as p

def main():

   ### Part 2: Playlist
   test_playlist_basics()
   test_are_equal()
   test_get_status()
   test_next_song()

   print(“TEST RESULTS:”, passed, “/”,tests)

def test_playlist_basics():
   print(“testing playlist basics”)
   list1 = []
   list2 = [s.Song(“Champions”, “Queen”, 220),
           s.Song(“Africa”,”Toto”, 295),
           s.Song(“HeyJude”, “The Beatles”, 431),
           s.Song(“Like aPrayer”, “Madonna”, 319)]

   list3 = [s.Song(“Someone You Loved”, “LewisCapaldi”, 182),
          s.Song(“Circles”, “Post Malone”, 215),
           s.Song(“TruthHurts”, “Lizzo”, 173),
           s.Song(“OnlyHuman”, “Jonas Brothers”, 183),
          s.Song(“Senorita”, “Shawn Mendes”, 191),
          s.Song(“Beautiful People”, “Ed Sheeren”, 207),
          s.Song(“Goodbyes”, “Post Malone”, 175)]

   playlist1 = p.Playlist(“None”, list1)
   playlist2 = p.Playlist(“Oldies”, list2)
   playlist3 = p.Playlist(“Top 40”, list3)

   # Part a:
   print(playlist1) # what should be outputhere?
   print(playlist2) # what should be outputhere?
   print(playlist3) # what should be outputhere?

   # Part b:
   # TODO: add code here to update the name ofplaylist2 to “Classics”
   print(playlist2) # This output should now bedifferent

   # Part c:
   result = playlist1.get_duration()
   print_test(“testing with playlist1”, result ==0)
   # add a test for playlist2’s duration
   # add a test for playlist3’s duration

   # Part d:
   # add a song to a playlist and test that theplaylist
   # has been updated appropriately (try addingsongs
   # that already exist too). What attributes of aplaylist
   # change when a song is added?

def test_are_equal():
   print(“testing are_equal”)

   list1 = []
   list2 = [s.Song(“Champions”, “Queen”, 220),
           s.Song(“Africa”,”Toto”, 295),
           s.Song(“HeyJude”, “The Beatles”, 431),
           s.Song(“Like aPrayer”, “Madonna”, 319)]
   list3 = [s.Song(“Africa”, “Toto”, 295),
          s.Song(“Champions”, “Queen”, 220),
           s.Song(“Like aPrayer”, “Madonna”, 319),
           s.Song(“HeyJude”, “The Beatles”, 431)]
   list4 = [s.Song(“Someone You Loved”, “Lewis Capaldi”,182),
          s.Song(“Circles”, “Post Malone”, 215),
           s.Song(“TruthHurts”, “Lizzo”, 173),
           s.Song(“OnlyHuman”, “Jonas    Brothers”, 183),
          s.Song(“Senorita”, “Shawn Mendes”, 191),
          s.Song(“Beautiful People”, “Ed Sheeren”, 207),
          s.Song(“Goodbyes”, “Post Malone”, 175)]

   playlist1 = p.Playlist(“None”, list1)
   playlist2 = p.Playlist(“Oldies”, list2)
   playlist3 = p.Playlist(“Favs”, list3)
   playlist4 = p.Playlist(“Top 40”, list4)

   # TODO: Add tests to determine if two
   # playlists are equal.

def test_get_status():
   print(“Testing get_status”)
   # TODO: Add tests to test the get_status
   # method found in the Playlist.py class

def test_next_song():
  
print(“Testing next_song”)
   # TODO: Add tests to test thenext_song()
   # method found in the Playlist.py class

# (str, bool -> None)
# takes the name or description of a test and whether the
# test produced the expected output (True) or not (False)
# and prints out whether that test passed or failed
# NOTE: You should not have to modify this in any way.
def print_test(test_name, result_correct):
   global tests
   global passed
   tests += 1
   if(result_correct):
       print(test_name + “: passed”)
       passed += 1
   else:
       print(test_name + “: failed”)

# The following code will call your main function
if __name__ == ‘__main__’:
   main()

———————————–

Playlist.py:
class Playlist:
   # (str, str, (list of Song) -> None)
   # constructor for the Playlist class
   def __init__(self, name, songs):
       self.name = name
       self.songs = songs
       self.duration =self.calc_duration()

   # (None -> str)
   # return a string with the name and duration of theplaylist
   def __str__(self):
       return self.name + ” (” +str(self.duration) + “)”

   # (None -> str)
   # return a string with the name of the playlist
   def __repr__(self):
       return self.name

   # (Playlist -> bool)
   # return True if the other playlist contains
   # all of the exact same songs, False otherwise
   def __eq__(self, other):
       print(“Fix me”)

   # (None -> str)
   # return name of the playlist
   def get_name(self):
       return self.name

   # (str -> None)
   # update the name of the playlist to new_name
   def set_name(self, new_name):
       self.name = new_name

   # (None -> (list of Song))
   # return the list of songs in the playlist
   def get_songs(self):
       return self.songs

   # ((list of Song)) -> None)
   # change the list of songs in the playlist to thegiven list
   def set_songs(self, song_list):
       self.songs = song_list
       self.duration = calc_duration()

   # (None -> int)
   # return the duration of the playlist
   def get_duration(self):
       return self.duration

   # (None -> int)
   # return the sum of all song durations
   def calc_duration(self):
       sum = 0
       for s in self.songs:
           sum +=s.get_duration()
       return sum

   # (Song -> None)
   # add new_song to the songs list if it is not alreadyin the playlist
   def add_song(self, new_song):
       if new_song not inself.songs:
          self.songs.append(new_song)
           self.duration =self.calc_duration()

   # (Song -> None)
   # remove a song from the songs list
   def remove_song(self, song):
       if song in self.songs:
          self.songs.remove(song)
       self.duration =self.calc_duration()

   # (int -> tuple)
   # given an amount of time the playlist has been
   # playing, return a tuple of the form (str, int)
   # containing the name of the current song, and
   # how many seconds the song has been playing
   def get_status(self, current):
       print(“Fix me!”)

   # (Song -> Song)
   # given a song, return the next song in theplaylist
   # Assume the given song is in the playlist
   def next_song(self, title):
       print(“Fix me!”)

Expert Answer


Answer to USING PYTHON 3.7: Part 2: The Playlist class Exercise 3 – Exploring the Playlist class Read through the Playlist.py cl…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?