-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.py
More file actions
52 lines (41 loc) · 1.91 KB
/
Copy pathplaylist.py
File metadata and controls
52 lines (41 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import song
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def add_song(self, song):
if song not in self.songs:
self.songs.append(song)
print(f"{song.title} added to {self.name} playlist.")
else:
print(f"{song.title} is already in the {self.name} playlist.")
def display(self):
print(f"\nPlaylist: {self.name}")
for song in self.songs:
print(f" - {song.title} by {song.artist_name} ({song.genre})")
def sort_by(self, attribute):
try:
self.songs.sort(key=lambda song: getattr(song, attribute))
print(f"{self.name} playlist sorted by {attribute}.")
except AttributeError:
print(f"Cannot sort by '{attribute}'. Attribute not found.")
def sort_by_mood(self, target_mood):
matching = [song for song in self.songs if target_mood in song.mood_tags]
others = [song for song in self.songs if target_mood not in song.mood_tags]
self.songs = matching + others
print(f"{self.name} playlist sorted by mood tag '{target_mood}'.")
def sort_by_mood_and_attributes(self, target_mood):
def attribute_key(song):
return (song.valence, song.energy, song.danceability, song.tempo)
matching = [song for song in self.songs if target_mood in song.mood_tags]
others = [song for song in self.songs if target_mood not in song.mood_tags]
matching.sort(key=attribute_key)
others.sort(key=attribute_key)
self.songs = matching + others
print(f"{self.name} playlist sorted by mood '{target_mood}' and musical attributes.")
def remove_song(self, song):
if song in self.songs:
self.songs.remove(song)
print(f"{song.title} removed from {self.name} playlist.")
else:
print(f"{song.title} not found in {self.name} playlist.")