Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/__pycache__/song.cpython-313.pyc
Binary file not shown.
53 changes: 52 additions & 1 deletion lib/song.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
class Song:
pass
# Class attributes
count = 0
genres = []
artists = []
genre_count = {}
artist_count = {}

def __init__(self, name, artist, genre):
"""Initialize a new Song instance with name, artist, and genre."""
self.name = name
self.artist = artist
self.genre = genre

# Call class methods to update class attributes
self.add_song_to_count()
self.add_to_genres(genre)
self.add_to_artists(artist)
self.add_to_genre_count(genre)
self.add_to_artist_count(artist)

@classmethod
def add_song_to_count(cls):
"""Increment the total count of songs."""
cls.count += 1

@classmethod
def add_to_genres(cls, genre):
"""Add genre to genres list if it's not already present."""
if genre not in cls.genres:
cls.genres.append(genre)

@classmethod
def add_to_artists(cls, artist):
"""Add artist to artists list if it's not already present."""
if artist not in cls.artists:
cls.artists.append(artist)

@classmethod
def add_to_genre_count(cls, genre):
"""Update the count of songs for each genre."""
if genre in cls.genre_count:
cls.genre_count[genre] += 1
else:
cls.genre_count[genre] = 1

@classmethod
def add_to_artist_count(cls, artist):
"""Update the count of songs for each artist."""
if artist in cls.artist_count:
cls.artist_count[artist] += 1
else:
cls.artist_count[artist] = 1
44 changes: 44 additions & 0 deletions test_song.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))

from song import Song

# Reset class attributes for testing
Song.count = 0
Song.genres = []
Song.artists = []
Song.genre_count = {}
Song.artist_count = {}

# Test basic functionality
print("Testing Song class implementation...")

# Create some songs
song1 = Song("99 Problems", "Jay Z", "Rap")
song2 = Song("Halo", "Beyonce", "Pop")
song3 = Song("Smells Like Teen Spirit", "Nirvana", "Rock")
song4 = Song("Out of Touch", "Hall and Oates", "Pop")

# Test instance attributes
print(f"Song1 name: {song1.name}")
print(f"Song1 artist: {song1.artist}")
print(f"Song1 genre: {song1.genre}")

# Test class attributes
print(f"Total song count: {Song.count}")
print(f"Genres: {Song.genres}")
print(f"Artists: {Song.artists}")
print(f"Genre count: {Song.genre_count}")
print(f"Artist count: {Song.artist_count}")

# Create another song to test counting
song5 = Song("Sara Smile", "Hall and Oates", "Pop")
print(f"After adding another song:")
print(f"Total song count: {Song.count}")
print(f"Genre count: {Song.genre_count}")
print(f"Artist count: {Song.artist_count}")

print("All tests completed successfully!")