"""File: movie_db2.py Purpose: Create, print, modify and query a database of movies stored as a dictionary Run: python movie_db2.py Input: Queries to the database Output: Results of queries """ from movie_d_use import * from movie_db import * #---------------------------------------------------------------------- def menu(): print "You can" print " - Add a new movie to the database (a)" print " - Delete a movie from the database (d)" print " - Print info on a movie in the database (p)" print " - Print titles of movies in the database (t)" print " - Print movies in which an actor starred (s)" print " - Read the database from a file(r)" print " - Write the database to a file (w)" print " - Print this menu (m)" print " - Quit (q)" #---------------------------------------------------------------------- def add_movie(m_db): """Add a movie to the database""" title = raw_input("What's the title of the movie?\n ") m_db[title] = create_movie_d(title) #---------------------------------------------------------------------- def del_movie(m_db): """Delete a movie from the database""" title = raw_input("What's the title of the movie?\n ") if m_db.has_key(title): del m_db[title] else: print "Sorry,", title, "isn't in the database" #---------------------------------------------------------------------- def print_movie(m_db): """Print all information on one movie""" title = raw_input("What's the title of the movie?\n ") if m_db.has_key(title): print_movie_d(m_db[title]) else: print "Sorry,", title, "isn't in the database" #---------------------------------------------------------------------- def print_movie_titles(m_db): """Print titles of all movies in the database""" for title in m_db: print title #---------------------------------------------------------------------- def star_in_movie(star, movie): """Linear search of actors in movie for star""" star_list = movie["Stars"] for actor in star_list: if actor == star: return True return False #---------------------------------------------------------------------- def print_stars_movies(m_db): """Print movies in which an actor starred""" star = raw_input("Who is the actor?\n ") movies = [] for title in m_db: if star_in_movie(star, m_db[title]): movies.append(title) print star, "starred in:" for title in movies: print " ", title #---------------------------------------------------------------------- def write_movie(movie, ofile): """Write info on a single movie to ofile The info is stored in the following format: Title title Year year Director director Stars number of stars star1 star2 . . . Rating rating So each value is preceded by its key""" for key in movie: ofile.write(key + "\n") if key != "Stars": data = str(movie[key]) ofile.write(data + "\n") else: # key == "Stars" ofile.write(str(len(movie["Stars"])) + "\n") for actor in movie["Stars"]: ofile.write(actor + "\n") #---------------------------------------------------------------------- def write_movie_db(m_db): """Write the movie database out to a file""" ofilename = raw_input("What's the name of the file?\n ") ofile = open(ofilename, "w") # print "ofile =", ofile for movie in m_db: write_movie(m_db[movie], ofile) ofile.close() #---------------------------------------------------------------------- def read_star_list(ifile): """Read the stars in a movie from ifile. Return a list of the stars. Precondition: ifile is at an int indicating the number of stars.""" str_n = ifile.readline() str_n = str_n.strip() n = int(str_n) # print "n =", n star_list = [] for i in range(n): actor = ifile.readline() actor = actor.strip() star_list.append(actor) # print "Returning", star_list return star_list #---------------------------------------------------------------------- def read_movie(ifile): """Read info on a movie. Return a movie dictionary Precondition: ifile is at the beginning of a block of movie information. Postcondition: ifile is at the beginning of a new block of movie or at end-of-file""" movie = dict() for i in range(5): # Five keys key = ifile.readline() key = key.strip() # Get rid of newline if key != "Stars": val = ifile.readline() val = val.strip() if key == "Year" or key == "Rating": intval = int(val) # print "key =", key, "val =", intval movie[key] = intval else: # print "key =", key, "val =", val movie[key] = val else: # key == "Stars" movie[key] = read_star_list(ifile) # print "key =", key, "val =", movie[key] return movie #---------------------------------------------------------------------- def read_movie_db(): """Read a movie database created by write_movie_db""" ifilename = raw_input("What's the name of the file?\n ") ifile = open(ifilename, "r") m_db = dict() key = ifile.readline() while key != "": ifile.seek(-len(key),1) # Go back to the start of key movie = read_movie(ifile) title = movie["Title"] m_db[title] = movie key = ifile.readline() return m_db #---------------------------------------------------------------------- def query_update_db(m_db): """Query and/or update the database""" menu() cmd = raw_input("(a, d, p, t, s, r, w, m, q)\n ") while cmd != 'q' and cmd != 'Q': if cmd == 'a' or cmd == 'A': add_movie(m_db) elif cmd == 'd' or cmd == 'D': del_movie(m_db) elif cmd == 'p' or cmd == 'P': print_movie(m_db) elif cmd == 't' or cmd == 'T': print_movie_titles(m_db) elif cmd == 's' or cmd == 'S': print_stars_movies(m_db) elif cmd == 'r' or cmd == 'R': m_db = read_movie_db() elif cmd == 'w' or cmd == 'W': write_movie_db(m_db) elif cmd == 'm' or cmd == 'M': menu() else: print cmd, "isn't a valid option" menu() print "Please try again" cmd = raw_input("(a, d, p, t, s, r, w, m, q)\n ") #---------------------------------------------------------------------- # Main program # m_db = create_movie_db() # print_movie_db(m_db) m_db = dict() query_update_db(m_db)