Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Demonstration script for handling a Dictionary in Python, used as a mini database
- # Mike Kerry - Jan 2021 - acclivity2@gmail.com
- # This dictionary will be indexed by Musical Artist
- # Each Artist entry will hold a list of Albums by that artist
- # Album data will include Date of Release and the medium (e.g. EP/LP/CD/MP3/Video)
- # Example format of the Dictionary
- # {'BEATLES': ['Beatles', ['ABBEY ROAD', 'Abbey Road', '1969', 'CD'], ['LET IT BE', 'Let It Be', '1970', 'CD'], ['HELP', 'Help', '1965', 'LP']],
- # 'WHO': ['Who', ['MY GENERATION', 'My Generation', '1965', 'CD'], ['WHO', 'Who', '2019', 'CD']],
- # 'FRANK SINATRA': ['Frank Sinatra', ['MY WAY', 'My way', '1969', 'LP']]}
- # The dictionary data will be backed up in a CSV file, with one line per album
- # Beatles,Abbey Road,1969,CD
- # Beatles,Let It Be,1970,CD
- # Beatles,Help,1965,LP
- # Who,My Generation,1965,CD
- # Who,Who,2019,CD
- # Frank Sinatra,My way,1969,LP
- def normalise_artist(p_artist):
- # This routine can be used if required, to remove "The " off the front of an Artist name
- # Then, for example, "The Beatles" and "Beatles" are treated as the same group
- p_artist = p_artist.title()
- if p_artist.upper()[:4] == "THE ":
- return p_artist[4:]
- return p_artist
- def add_artist(p_artist):
- global music_dict
- # We will make the Artist Key all upper-case to avoid case discrepancies on look-up
- p_artist_key = p_artist.upper()
- if p_artist_key not in music_dict:
- # Create a new entry. For now it only contains the original form of the artist name
- music_dict[p_artist_key] = [p_artist]
- return True
- else:
- return False # Return False to say add was not possible
- def add_album(p_artist, p_album, p_date, p_medium):
- global music_dict
- # Make the album key all upper case
- p_artist_key = p_artist.upper()
- p_album_key = p_album.upper()
- t_artist_data = music_dict[p_artist_key]
- # The above statement extracts all the data held for one artist into a list named t_artist_data
- # This list holds the original artist name, followed by a sub-list with one entry per album
- # Example: ['Who', ['MY GENERATION', 'My Generation', '1965', 'CD'], ['WHO', 'Who', '2019', 'CD']]
- # Existing albums are elements 1 upwards within the artist data
- for t_idx in range(1, len(t_artist_data)):
- t_alist = t_artist_data[t_idx]
- if t_alist[0] == p_album_key: # Does the album name we want to add already exist?
- return False # - yes - return saying add failed
- t_artist_data += [[p_album_key, p_album, p_date, p_medium]] # append a new sub-list to the artist data
- music_dict[p_artist_key] = t_artist_data # insert the extended data back into the database for this artist
- return True # Return a flag saying album was added successfully
- music_dict = {}
- # Read text file (CSV format) and build Dictionary
- fin = open("MusicData.txt")
- for lina in fin:
- lina = lina.strip()
- splita = lina.split(",")
- if len(splita) < 3:
- continue # Ignore incomplete lines
- for x, el in enumerate(splita):
- splita[x] = el.strip()
- # Extract the parts from one album line
- artist = splita[0].title()
- artist = normalise_artist(artist)
- album = splita[1]
- date = splita[2]
- medium = splita[3]
- print(artist, album, date, medium)
- # Add this album to our dictionary
- add_artist(artist)
- add_album(artist, album, date, medium)
- fin.close()
- print(music_dict)
- while True:
- print("1. Add new artist")
- print("2. Add new album")
- print("3. List albums for an artist")
- print("4. Find album")
- print("9. Quit")
- option = input("Choose an option: ")
- if option == "1":
- # New artist
- artist = input("Enter Artist name: ")
- artist = normalise_artist(artist)
- res = add_artist(artist)
- if res is False:
- print("That artist is already in the database")
- else:
- print("Artist", artist, "added to database")
- continue
- if option == "2":
- artist = input("Enter Artist for this album: ")
- artist = normalise_artist(artist)
- artist_key = artist.upper()
- if artist_key not in music_dict:
- print("That artist is not in the database")
- continue
- album = input("Enter Album Name: ")
- album_key = album.upper()
- date = input("Enter Release Date: ")
- medium = input("Enter Medium (EP/LP/CD: ")
- res = add_album(artist, album, date, medium)
- if res is False:
- print("That album is already in the database")
- else:
- print("Album has been added")
- continue
- if option == "3":
- artist = input("Enter Artist Name: ")
- artist = normalise_artist(artist)
- artist_key = artist.upper()
- if artist_key not in music_dict:
- print("That artist is not in the database")
- continue
- # List albums for the artist
- artist_data = music_dict[artist_key]
- # artist_data contains original artist name + a list of lists of albums
- # Existing albums are elements 1 upwards within the artist data
- for idx in range(1, len(artist_data)):
- alist = artist_data[idx]
- album = alist[1]
- date = alist[2]
- medium = alist[3]
- print(artist.ljust(15), album.ljust(20), date.ljust(8), medium.ljust(6))
- if option == "4":
- # Search all Artists for an Album name
- album = input("Enter Album Name: ")
- album_key = album.upper()
- found = False
- for artist_key in music_dict.keys():
- artist_data = music_dict[artist_key]
- for x in range(1, len(artist_data)):
- album_data = artist_data[x]
- if album_data[0] == album_key:
- found = True
- artist = artist_data[0]
- album = album_data[1]
- date = album_data[2]
- medium = album_data[3]
- print(artist.ljust(15), album.ljust(20), date.ljust(8), medium.ljust(6))
- if not found:
- print("No album named", album, "was found")
- if option == "9":
- # Quit. Write the dictionary back out to the text file in CSV format
- fout = open("MusicData.txt", 'w')
- for artist_key in music_dict.keys():
- artist_data = music_dict[artist_key]
- artist = artist_data[0]
- for x in range(1, len(artist_data)):
- album_data = artist_data[x]
- album = album_data[1]
- date = album_data[2]
- medium = album_data[3]
- outline = ",".join([artist, album, date, medium]) + "\n"
- fout.write(outline)
- fout.close()
- break # Quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement