Esempio n. 1
0
def addAlbum(inDict, dbFilePath):
    name = input('Please enter the name of the album: ')
    while True:
        try:
            year = int(input('Please enter release year: '))
        except:
            print('Year must be an integer!')
            continue
        break
    artist = input('Please enter the artist: ')
    newAlbum = Album(name, artist, year)
    while True:
        newSong = input('Please add a song or type "q" to stop: ')
        if (newSong == 'q'):
            break
        else:
            newAlbum.appendSong(newSong)
    if (newAlbum.getArtist() not in inDict):
        inDict[newAlbum.getArtist()] = []
        inDict[newAlbum.getArtist()].append(newAlbum)
    else:
        albumFound = False
        for album in inDict[newAlbum.getArtist()]:
            if (album.getName() == newAlbum.getName()):
                albumFound = True
        if (albumFound):
            print('Album already exists! Aborting.')
        else:
            inDict[newAlbum.getArtist()].append(newAlbum)
            dict2File(inDict, dbFilePath)
Esempio n. 2
0
def file2Dict(filePath):
    d = dict()
    dbFile = open(filePath, 'r')
    dbText = dbFile.read()
    dbtext = dbText.strip()
    dbList = dbText.split("\n\n")
    for album in dbList:
        albumLines = album.split("\n")
        i = 0
        for albumLine in albumLines:
            if (i == 0):
                artist = albumLine.strip()
            elif (i == 1):
                words = albumLine.split()
                year = words[0].strip()
                name = albumLine.strip(year)
                name = name.strip()
                year = int(year)
                newAlbum = Album(name, artist, year)
            else:
                song = albumLine.strip('-')
                song = song.strip()
                if (song == ''):
                    continue  #splitting on last line will create blank song, ignore this
                else:
                    newAlbum.appendSong(song)
            i += 1
        if (newAlbum.getArtist() in d):
            d[newAlbum.getArtist()].append(newAlbum)
        else:
            d[newAlbum.getArtist()] = []
            d[newAlbum.getArtist()].append(newAlbum)
    return d