-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylistiterator.py
More file actions
53 lines (43 loc) · 1.35 KB
/
Copy pathplaylistiterator.py
File metadata and controls
53 lines (43 loc) · 1.35 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
53
import random
class Song:
def __init__(self, title, artist):
self.title = title
self.artist = artist
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def add_song(self, song):
self.songs.append(song)
def __iter__(self):
return PlaylistIterator(self.songs)
class PlaylistIterator:
def __init__(self, songs , mode="Normal"): #Normal,Shuffle or Repeat
self.songs = songs
self.index = 0
self.mode = mode
if self.mode == "Shuffle":
random.shuffle(self.songs)
def __next__(self):
if self.index < len(self.songs):
song = self.songs[self.index]
self.index += 1
return song
elif self.mode == "Repeat" and self.index >= len(self.songs):
self.index = 0
song = self.songs[self.index]
self.index += 1
return song
else:
raise StopIteration
Music1 = Song("Song 1", "Artist 1")
Music2 = Song("Song 2", "Artist 2")
Music3 = Song("Song 3", "Artist 3")
Music4 = Song("Song 4", "Artist 4")
my_playlist = Playlist("My Playlist")
my_playlist.add_song(Music1)
my_playlist.add_song(Music2)
my_playlist.add_song(Music3)
my_playlist.add_song(Music4)
for song in my_playlist:
print(f"Title: {song.title}, Artist: {song.artist}")