music-playlist / song.py
Amanda Torres
initial commit
e830c88
"""
Song Class
Represents a music track
"""
class Song:
def __init__(self, song_id, title, artist, album):
self.song_id = song_id
self.title = title
self.artist = artist
self.album = album
self.duration = 0
self.genre = "Unknown"
self.year = None
self.play_count = 0
self.rating = 0.0
self.favorite = False
self.file_path = ""
def set_duration(self, seconds):
"""
Set song duration in seconds
"""
if seconds > 0:
self.duration = seconds
return True
return False
def set_genre(self, genre):
"""
Set music genre
"""
self.genre = genre
def set_year(self, year):
"""
Set release year
"""
if 1900 <= year <= 2100:
self.year = year
return True
return False
def play(self):
"""
Increment play count
"""
self.play_count += 1
def rate(self, rating):
"""
Rate the song (1-5 stars)
"""
if 1 <= rating <= 5:
self.rating = rating
return True
return False
def toggle_favorite(self):
"""
Toggle favorite status
"""
self.favorite = not self.favorite
def get_duration_string(self):
"""
Get duration as MM:SS string
"""
minutes = self.duration // 60
seconds = self.duration % 60
return f"{minutes}:{seconds:02d}"
def to_dict(self):
"""
Convert song to dictionary
"""
return {
'song_id': self.song_id,
'title': self.title,
'artist': self.artist,
'album': self.album,
'duration': self.get_duration_string(),
'genre': self.genre,
'year': self.year,
'play_count': self.play_count,
'rating': self.rating,
'favorite': self.favorite
}
def __str__(self):
return f"{self.artist} - {self.title} ({self.get_duration_string()})"