In this final day of this project, we will learn to sort data, modify data, and remove data from the collection.
import os, pickle
class clSong:
def __init__(self, strTitle, strRating, strArtist, intYear):
self.strTitle = strTitle
self.strRating = strRating
self.strArtist = strArtist
self.intYear = intYear
def __str__(self):
return f"{self.strTitle}".ljust(20)+f"{self.strRating}".ljust(12)+f"{self.strArtist}".ljust(20)+f"{self.intYear}".rjust(8)
def getTitle(self):
return self.strTitle
def getRating(self):
return self.strRating
def getArtist(self):
return self.strArtist
def getYear(self):
return self.intYear
def setRating(self):
print(f"\nThe current rating is: {self.strRating}")
strNew = input("What should the new rating be? (* - *****)")
boolValid = True
for strChar in strNew:
if strChar != "*":
boolValid = False
break
if len(strNew) < 1 or len(strNew) > 5:
boolValid = False
if boolValid == True:
self.strRating = strNew
print("Your rating has been updated.")
else:
print("Your input was not valid.")
class clPlaylist:
def __init__(self):
if os.path.exists("playlist_data.txt"):
file = open("playlist_data.txt", "rb")
self.liPlaylist = pickle.load(file)
file.close()
else:
self.liPlaylist = []
self.saveData()
def addSong(self):
strT = input("What is the title? ")
while True:
strR = input("What is the rating? (* - *****): ")
boolValid = True
for strChar in strR:
if strChar != "*":
boolValid = False
break
if len(strR) < 1 or len(strR) > 5:
boolValid = False
if boolValid == True:
break
else:
print("Your input was not valid.")
print("Rating should be * to *****\n")
strA = input("Who is the artist? ")
while True:
intY = input("What is the year?")
if len(intY) == 4 and intY.isnumeric():
intY = int(intY)
break
else:
print("\nYears must be four digits.")
self.liPlaylist.append(clSong(strT, strR, strA, intY))
self.saveData()
def listSongs(self):
print(f"Title".ljust(20)+f"Rating".ljust(12)+f"Artist".ljust(20)+f"Year".rjust(8))
print("-"*60)
for song in self.liPlaylist:
print(song)
def saveData(self):
file = open("playlist_data.txt", "wb")
pickle.dump(self.liPlaylist, file)
file.close()
print("*"*60)
print("Welcome to my Playlist".center(60))
print("*"*60+"\n")
myPL = clPlaylist()
while True:
print("""
-----------------
Main Menu
-----------------
1. List Songs
2. Add a Song
3. Remove a Song
4. Change a Rating
0. Exit the Program
""")
strMenu = input("What would you like to do? ")
if strMenu == "1":
myPL.listSongs()
print("\n")
elif strMenu == "2":
myPL.addSong()
print("\n")
elif strMenu == "3":
pass
elif strMenu == "4":
pass
elif strMenu == "0":
print("Thanks for using my Playlist App!")
break
else:
print("That's not an option.")
#
# myPL.addSong()
# myPL.addSong()
#
# myPL.listSongs()
import os, pickle
class clSong:
def __init__(self, strTitle, strRating, strArtist, intYear):
self.strTitle = strTitle
self.strRating = strRating
self.strArtist = strArtist
self.intYear = intYear
def __str__(self):
return f"{self.strTitle}".ljust(19)+f"{self.strRating}".ljust(11)+f"{self.strArtist}".ljust(19)+f"{self.intYear}".rjust(7)
def getTitle(self):
return self.strTitle
def getRating(self):
return self.strRating
def getArtist(self):
return self.strArtist
def getYear(self):
return self.intYear
def setRating(self):
print(f"\nThe current rating is: {self.strRating}")
strNew = input("What should the new rating be? (* - *****)")
boolValid = True
for strChar in strNew:
if strChar != "*":
boolValid = False
break
if len(strNew) < 1 or len(strNew) > 5:
boolValid = False
if boolValid == True:
self.strRating = strNew
print("Your rating has been updated.")
else:
print("Your input was not valid.")
class clPlaylist:
def __init__(self):
if os.path.exists("playlist_data.txt"):
file = open("playlist_data.txt", "rb")
self.liPlaylist = pickle.load(file)
file.close()
else:
self.liPlaylist = []
self.saveData()
def addSong(self):
strT = input("What is the title? ")
while True:
strR = input("What is the rating? (* - *****): ")
boolValid = True
for strChar in strR:
if strChar != "*":
boolValid = False
break
if len(strR) < 1 or len(strR) > 5:
boolValid = False
if boolValid == True:
break
else:
print("Your input was not valid.")
print("Rating should be * to *****\n")
strA = input("Who is the artist? ")
while True:
intY = input("What is the year?")
if len(intY) == 4 and intY.isnumeric():
intY = int(intY)
break
else:
print("\nYears must be four digits.")
self.liPlaylist.append(clSong(strT, strR, strA, intY))
self.sortSongs()
self.saveData()
def removeSong(self):
self.listSongs()
print("""
What song do you want to remove?
Type the number of the song to remove
Type 'x' to go back
""")
strRem = input("What do you want to do? ")
if strRem == "x":
pass
elif strRem.isnumeric():
intRem = int(strRem)
if isinstance(self.liPlaylist[intRem],clSong):
objRem = self.liPlaylist.pop(intRem)
print(f"The song, {objRem.getTitle()} has been removed.")
else:
print("That song doesn't exist.")
else:
print("Invalid input.")
self.saveData()
def changeRating(self):
self.listSongs()
print("""
What song do you want to change?
Type the number of the song to change rating
Type 'x' to go back
""")
strRem = input("What do you want to do? ")
if strRem == "x":
pass
elif strRem.isnumeric():
intRem = int(strRem)
if isinstance(self.liPlaylist[intRem], clSong):
obSong = self.liPlaylist[intRem]
print(f"What rating do you give {obSong.getTitle()}?")
obSong.setRating()
self.saveData()
else:
print("That song doesn't exist.")
else:
print("Invalid input.")
self.saveData()
def listSongs(self):
print(" "+f"Title".ljust(19)+f"Rating".ljust(11)+f"Artist".ljust(19)+f"Year".rjust(7))
print("-"*60)
intIndex = 0
for song in self.liPlaylist:
print(f"{intIndex}".ljust(4), end="")
print(song)
intIndex += 1
def saveData(self):
file = open("playlist_data.txt", "wb")
pickle.dump(self.liPlaylist, file)
file.close()
def sortSongs(self):
self.liPlaylist.sort(key=lambda song: song.getTitle().lower())
print("*"*60)
print("Welcome to my Playlist".center(60))
print("*"*60+"\n")
myPL = clPlaylist()
while True:
print("""
-----------------
Main Menu
-----------------
1. List Songs
2. Add a Song
3. Remove a Song
4. Change a Rating
0. Exit the Program
""")
strMenu = input("What would you like to do? ")
if strMenu == "1":
myPL.listSongs()
print("\n")
elif strMenu == "2":
myPL.addSong()
print("\n")
elif strMenu == "3":
myPL.removeSong()
print("\n")
elif strMenu == "4":
myPL.changeRating()
print("\n")
elif strMenu == "0":
print("Thanks for using my Playlist App!")
break
else:
print("That's not an option.")
#
# myPL.addSong()
# myPL.addSong()
#
# myPL.listSongs()