示例#1
0
def load_data():
    new_artist = None
    new_album = None
    artist_list = []

    with open("albums.txt", "r") as albums:
        for line in albums:
            artist_field, album_field, year_field, song_field = tuple(
                line.strip('\n').split('\t'))
            year_field = int(year_field)
            print(artist_field, album_field, year_field, song_field)

            if new_artist is None:
                new_artist = Artist(artist_field)
            elif new_artist.name != artist_field:
                new_artist.add_album(new_album)
                artist_list.append(new_artist)
                new_artist = Artist(artist_field)
                new_album = None

            if new_album is None:
                new_album = Album(album_field, year_field, new_artist)
            elif new_album.name != album_field:
                new_artist.add_album(new_album)
                new_album = Album(album_field, year_field, new_artist)

            new_song = Song(song_field, new_artist)
            new_album.add_song(new_song)

        if new_artist is not None:
            if new_album is not None:
                new_artist.add_album(new_album)
            artist_list.append(new_artist)

    return artist_list
 def test_adding_song_raises_error_when_artists_doesnt_match(self):
     a = Artist('Lana Del Ray', 'USA')
     a1 = Artist('Adele', 'UK')
     b = Album('Born to Die', 2012, 'alternative/indie', a)
     b1 = Album('21', 2011, 'pop/soul', a1)
     c = Song('Blue Jeans', a, 2012, 240, b)
     with self.assertRaises(WrongArtistError):
         b1.add_song(c)