informaticage

Playlist

Feb 4th, 2021 (edited)
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import random
  2. import json
  3.  
  4.  
  5. class MusicPiece(dict):
  6.     def __init__(self, author, title, length):
  7.         dict.__init__(self, author = author, title = title, length = length)
  8.         self.author = author
  9.         self.title = title
  10.         self.length = length
  11.  
  12.     def __eq__(self, other):
  13.         return self.title == other.title
  14.  
  15.     def __str__(self):
  16.         return f'{self.author} - {self.title} - {self.length}sec'
  17.  
  18.     def toJson(self):
  19.         return json.dumps(self.__dict__)
  20.  
  21.  
  22. class Playlist(dict):
  23.     musicPieces = None
  24.  
  25.     def __init__(self, title):
  26.         self.musicPieces = []
  27.         self.title = title
  28.  
  29.     def shuffle(self):
  30.         random.shuffle(self.musicPieces)
  31.  
  32.     def addMusicPiece(self, musicPiece):
  33.         if not isinstance(musicPiece, MusicPiece):
  34.             raise "Can only add music pieces to a playlist"
  35.         self.musicPieces.append(musicPiece)
  36.  
  37.     def deleteMusicPiece(self, title):
  38.         toRemove = MusicPiece('_', title, 0)
  39.         if(toRemove in self.musicPieces):
  40.             self.musicPieces.remove(toRemove)
  41.             return True
  42.         return False
  43.  
  44.     def sortByTitle(self, desc=False):
  45.         self.musicPieces.sort(key=lambda mp: mp.title, reverse=desc)
  46.  
  47.     def __str__(self):
  48.         return "\n".join([piece.__str__() for piece in self.musicPieces])
  49.  
  50.     def toJson(self):
  51.         dict.__init__(self, musicPieces = self.musicPieces, title=self.title)
  52.         return json.dumps(self.__dict__)
  53.  
  54. def main():
  55.     my_playlist = Playlist('Blues')
  56.     my_playlist.addMusicPiece(MusicPiece('Blues Brothers', 'Sweet Home Chicago', 180))
  57.     my_playlist.addMusicPiece(MusicPiece('Eric Clapton', 'Blues With Russel', 230))
  58.     print(my_playlist)
  59.  
  60.     print("-" * 50)
  61.     print(my_playlist.toJson())
  62.     print("-" * 50)
  63.  
  64.     my_playlist.deleteMusicPiece('Blues With Russel')
  65.     print(my_playlist)
  66.     print("-" * 50)
  67.  
  68.     jsonDump = '{\
  69.      "musicPieces":[\
  70.          {\
  71.            "author":"Blues Brothers",\
  72.            "title":"Sweet Home Chicago",\
  73.            "length":180\
  74.          },\
  75.          {\
  76.            "author":"Eric Clapton",\
  77.            "title":"Blues With Russel",\
  78.            "length":230\
  79.          }\
  80.      ],\
  81.      "title":"Blues"\
  82.    }'
  83.     newPlaylist = Playlist(json.loads(jsonDump))
  84.     print(newPlaylist)
  85.  
  86. if __name__ == "__main__":
  87.     main()
  88.  
Add Comment
Please, Sign In to add comment