"""File: movie_db1a.py Purpose: Create, print, modify and query a database of movies stored as a dictionary Run: python movie_db1a.py Input: Info on individual movies Queries to the database Output: List of movies and info Results of queries Note: This version adds some more reverse lookups """ 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 " - Print movies directed by a given director (c)" print " - Print movies produced in a given year (y)" print " - Print movies with a given rating (r)" 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 print_directors_movies(m_db): """Print movies directed by a given director""" dir = raw_input("Who is the director?\n ") movies = [] for title in m_db: if m_db[title]["Director"] == dir: movies.append(title) print dir, "directed:" for title in movies: print " ", title #---------------------------------------------------------------------- def print_movies_in_year(m_db): """Print movies produced in a given year""" year = int(raw_input("Which year?\n ")) movies = [] for title in m_db: if m_db[title]["Year"] == year: movies.append(title) print "Movies produced in", year, ":" for title in movies: print " ", title #---------------------------------------------------------------------- def print_movies_with_rating(m_db): """Print movies with a given rating""" rating = int(raw_input("What's the rating?\n ")) movies = [] for title in m_db: if m_db[title]["Rating"] == rating: movies.append(title) print "Movies with rating", rating, ":" for title in movies: print " ", title #---------------------------------------------------------------------- def query_update_db(m_db): """Query and/or update the database""" menu() cmd = raw_input("(a, d, p, t, s, c, y, r, 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 == 'c' or cmd == 'C': print_directors_movies(m_db) elif cmd == 'y' or cmd == 'Y': print_movies_in_year(m_db) elif cmd == 'r' or cmd == 'R': print_movies_with_rating(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, c, y, r, m, q)\n ") #---------------------------------------------------------------------- # Main program m_db = create_movie_db() print_movie_db(m_db) query_update_db(m_db)