Now that you’ve delved into creating class attributes and methods it is time to put these concepts to the test. In this lab our focus will be on a song class that will include several class attributes and methods.
Imagine you've just landed a role as a junior software engineer at MusicTech Innovations, a cutting-edge company that powers a popular music streaming service. Your first project involves enhancing the company's music library system. The goal is to design a Python class that encapsulates the essential properties and behaviors of a song, making it easier for the team to manage and analyze the vast collection of tracks.
You're tasked with creating a Song class that not only represents individual songs with attributes like name, artist, and genre but also maintains global insights. For example, your class will keep track of the total number of songs, list all unique artists and genres, and even count how many songs belong to each genre and artist. This functionality is critical for features like personalized recommendations and data analytics.
- GitHub Repo
- Python Documentation
- Classes - Python
- Python Class Attributes: An Overly Thorough Guide - Toptal
- Python's Instance, Class, and Static Methods Demystified - Real Python
- The Factory Method Pattern and Its Implementation in Python - Real Python
Before we begin coding, let's complete the initial setup for this lesson:
- Fork and Clone
- Go to the provided GitHub repository link.
- Fork the repository to your GitHub account.
- Clone the forked repository to your local machine.
- Open and Run File
- Open the project in VSCode.
- Run npm install to install all necessary dependencies.
Build a song class. As a user, one should be able to:
- Build a song object
- See information about all songs
- Use methods that will add to the songs
- Song
- Attributes
- name
- artist
- genre
- Class Attributes
- count
- genres
- artists
- genre_count
- artists_count
- Class Methods
- add_song_to_count
- add_to _genres
- add_to_artists
- add_to_genre_count
- add_to_artists_count
- Attributes
git checkout -b [name of branch]__init__:- name
- artist
- genre
- Class Attributes:
- We need our Song class to be able to keep track of the number of songs that it creates
- We need our Song class to be able to show us all of the artists of existing songs
- We need our Song class to be able to show us all of the genres of existing songs
- We also need our Song class to be able to keep track of the number of songs of each genre it creates
- Ex:
- {"Rap": 5, "Rock": 1, "Country": 3}
- Lastly, we want our Song class to reveal to us the number of songs each artist is responsible for
- Ex:
- {"Beyonce": 17, "Jay-Z": 40}
- count
- genres
- artists
- genre_count
- artists_count
- Each of the class methods should trigger upon the new song being created.
- add_song_to_count
- Increments the value of count by one
- add_to _genres
- Adds any new genres to a class attribute genres
- Ensure there are only unique genres - no duplicates!
- add_to_artists
- Adds any new artistes to a class attribute artists
- Ensure there are only unique artists - no duplicates!
- add_to_genre_count
- Updates class attribute genre_count
- Increments genre key by 1, if genre doesn’t exist in genre_count add the key and set it to 1
- add_to_artists_count
- Updates class attribute artists_count
- Increments artists key by 1, if artist doesn’t exist in artists_count add the key and set it to 1
- Push the branch to GitHub
- Create a Pull Request (PR) on GitHub.
- Merge the PR into main after review.
- Pull the new merged main branch locally and delete merged feature branch (optional)
Best Practice documentation steps:
- Add comments to code to explain purpose and logic
- Clarify intent / functionality of code to other developers
- Add screenshot of completed work included in Markdown in README.
- Update README text to reflect the functionality of the application following https://makeareadme.com.
- Delete any stale branches on GitHub
- Remove unnecessary/commented out code
- If needed, update git ignore to remove sensitive data
Before you submit your solution, you need to save your progress with git.
- Add your changes to the staging area by executing
git add. - Create a commit by executing
git commit -m "Your commit message" - Push your commits to GitHub by executing
git push origin mainorgit push origin master, depending on the name of your branch (usegit branchto check on which branch you are).
- Use the rubric in Canvas as a guide for how this lab is graded.
- Your submission will be automatically scored in CodeGrade, using the most recent commit. Remember to make sure you have pushed your commit to GitHub before submitting your assignment.
- You can review your submission in CodeGrade and see your final score in your Canvas gradebook.
- When you are ready to submit, click the Load Lab: Object Oriented Programming (OOP)- Part 1- Bookstore button in Canvas to launch CodeGrade.
- Click on + Create Submission. Connect your repository for this lab.
- For additional information on submitting assignments in CodeGrade: Getting Started in Canvas