Here’s the fully commented version of the VideoPlayer class, with simple and clear explanations for each line, avoiding complex methods:
class VideoPlayer:
"""A class to manage a playlist of videos."""
def __init__(self):
"""Initialize the VideoPlayer with an empty playlist."""
# Create an empty list to store videos
self.playlist = []
def add_video(self, video: 'Video') -> None:
"""
Add a video to the playlist if it's not already there.
Args:
video: The video object to add.
"""
# Check if a video with the same title exists
found = False
for v in self.playlist:
if v.title == video.title:
found = True
break
# Only add if not found
if not found:
self.playlist.append(video)
def remove_video(self, video: 'Video') -> None:
"""
Remove a video from the playlist.
Args:
video: The video object to remove.
"""
# Create a new list without the video to remove
new_playlist = []
for v in self.playlist:
if v.title != video.title:
new_playlist.append(v)
# Update the playlist
self.playlist = new_playlist
def get_duration_of_queue(self) -> int:
"""Calculate the total duration of all videos in the playlist."""
total = 0
for video in self.playlist:
total += video.duration
return total
def get_videos_by_author(self, author: str) -> list['Video']:
"""
Get all videos by a specific author (case-insensitive).
Args:
author: The author's name to search for.
Returns:
List of videos by the author.
"""
videos = []
for video in self.playlist:
# Compare lowercase versions for case-insensitive matching
if video.author.lower() == author.lower():
videos.append(video)
return videos
def sort_videos_alphabetically(self) -> list['Video']:
"""
Sort videos alphabetically by title, then by author if titles match.
Returns:
A new sorted list of videos.
"""
# Make a copy of the playlist to avoid modifying the original
sorted_list = self.playlist.copy()
# Bubble sort implementation
n = len(sorted_list)
for i in range(n):
for j in range(0, n-i-1):
# Compare titles (case-insensitive)
if sorted_list[j].title.lower() > sorted_list[j+1].title.lower():
# Swap if out of order
sorted_list[j], sorted_list[j+1] = sorted_list[j+1], sorted_list[j]
# If titles are equal, compare authors
elif sorted_list[j].title.lower() == sorted_list[j+1].title.lower():
if sorted_list[j].author.lower() > sorted_list[j+1].author.lower():
sorted_list[j], sorted_list[j+1] = sorted_list[j+1], sorted_list[j]
return sorted_list
def sort_videos_by_duration(self) -> list['Video']:
"""
Sort videos by duration (longest to shortest).
Returns:
A new sorted list of videos.
"""
# Make a copy of the playlist
sorted_list = self.playlist.copy()
# Bubble sort implementation
n = len(sorted_list)
for i in range(n):
for j in range(0, n-i-1):
if sorted_list[j].duration < sorted_list[j+1].duration:
# Swap if out of order
sorted_list[j], sorted_list[j+1] = sorted_list[j+1], sorted_list[j]
return sorted_list
def get_shortest_video(self) -> 'Video':
"""
Find and return the shortest video in the playlist.
Returns:
The video with the shortest duration.
Raises:
ValueError: If the playlist is empty.
"""
if not self.playlist:
raise ValueError("Playlist is empty")
# Initialize with first video
shortest = self.playlist[0]
# Compare with others
for video in self.playlist[1:]:
if video.duration < shortest.duration:
shortest = video
return shortest
def get_videos(self) -> list['Video']:
"""
Get a copy of all videos in the playlist.
Returns:
A copy of the playlist.
"""
# Return a new list with the same videos
return [video for video in self.playlist]