-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path067.Songs_Album.py
156 lines (126 loc) · 5.23 KB
/
067.Songs_Album.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class Song:
"""
Class to represent a Song
Attributes:
title (str): The title of the song
artist (str): the name of the song's creator
duration (int): The duration of th song in seconds. May be zero
"""
def __init__(self, title, artist, duration=0):
"""
Song init method
:param title: Initialises the 'title' attribute
:param artist: At Artist object representing the song's creator.
:param duration: Initial value for the 'duration' attribute.
Will default to zero if not specified
"""
self.title = title
self.artist = artist
self.duration = duration
def get_title(self):
return self.title
name = property(get_title)
class Album:
"""
Class to represent an Album, using it's track list
Attributes:
name (str):The name of the album
year (int): The year was album was released
artist (str): The name of the artist. If not specified.
the artist will default to an artist with the same "Various Artists".
tracks (List[Songs]): A List of Songs on the Album
"""
def __init__(self, name, year, artist="Various Artists"):
self.name = name
self.year = year
# if artist is None:
# self.artist = Artist("Various Artists")
# else:
# self.artist = artist
self.artist = artist
self.tracks = []
def add_song(self, song, position=None):
"""
Adds a song to the track list
:param song : (Song) : The title of the song to Add.
:param position: (Optional[int]) : It specified,the song will added to that position
in the track list - inserting it between other songs if necessary.
Otherwise, the song will added to the end of the list.
"""
song_found = find_object(song, self.tracks)
if song_found is None:
song_found = Song(song, self.artist)
if position is None:
self.tracks.append(song_found)
else:
self.tracks.insert(position, song_found)
class Artist:
"""
Basic class to store artist details.
Attributes:
name (str): The name of the artist
albums (List[Album]): A list of the albums by the artist.
The list includes only those albums in the collection, it is
not an exhaustive list of the artist's published albums.
Methods:
add_album: use to add album to the artist's albums list
"""
def __init__(self, name):
self.name = name
self.albums = []
def add_album(self, album):
"""
Add a new album to the list
:param album: Album object to add to the list.
If the album is already present, it will not added again (although this is yet to implemented).
"""
self.albums.append(album)
def add_song(self, name, year, title):
"""Add a new Song to the collection of albums
This method will add the song to an album in the collection.
A new album will be created in the collection if it doesn't exist.
Args:
name (str): The name of the album
year (int): Year the album was produced.
title (str): The title of the song
"""
album_found = find_object(name, self.albums)
if album_found is None:
print(name + " not found")
album_found = Album(name, year, self.name)
self.add_album(album_found)
else:
print("Found album " + name)
album_found.add_song(title)
def find_object(field, object_list):
"""Check 'object_list' to see if an object with a 'name' attribute equal to 'field' exists, return it if so"""
for item in object_list:
if item._name == field:
return item
return None
def load_data():
artist_list = []
with open("albums.txt", "r") as albums:
for line in albums:
# data row should consist of (artist, album, year, song)
artist_field, album_field, year_field, song_field = tuple(line.strip('\n').split('\t'))
year_field = int(year_field)
print("{}:{}:{}:{}".format(artist_field, album_field, year_field, song_field))
new_artist = find_object(artist_field, artist_list)
if new_artist is None:
new_artist = Artist(artist_field)
artist_list.append(new_artist)
new_artist.add_song(album_field, year_field, song_field)
return artist_list
def create_checkfile(artist_list):
""" Create a check file from the object data for comparison with original file"""
with open("checkfile.txt", 'w') as checkfile:
for new_artist in artist_list:
for new_album in new_artist.albums:
for new_song in new_album.tracks:
print("{0.name}\t{1.name}\t{1.year}\t{2.title}".format(new_artist, new_album, new_song),
file=checkfile)
if __name__ == '__main__':
artists = load_data()
print("There are {} artists".format(len(artists)))
create_checkfile(artists)