def test_empty_dict(): '''Test with an empty dictionary. ''' program_output = bacon_functions.invert_actor_dict({}) assert program_output == {},\ 'Empty dictionary test'
def test_single_key_single_value_dict(): '''Test with a single key dictionary which is the empty string. ''' program_output = bacon_functions.invert_actor_dict({'tart': ['sugar']}) assert program_output == {'sugar': ['tart']},\ 'Empty string key (dictionary) test'
def test_empty_dict_value(): '''Test with a single key dictionary mapping onto the empty list (value). ''' program_output = bacon_functions.invert_actor_dict({'cake': []}) assert program_output == {},\ 'Empty dictionary value test'
def test_single_key_multiple_values_dict(): '''Test with a dictionary containing a single key, with multiple items in the list (value) which it maps to. ''' program_output = bacon_functions.invert_actor_dict({'cake': ['icing','cherry']}) correct_output = { 'icing': ['cake'], 'cherry': ['cake'] } assert program_output == correct_output,\ 'Single key multiple value list dictionary test'
def test_single_key_multiple_values_dict(): '''Test with a dictionary containing a single key, with multiple items in the list (value) which it maps to. ''' program_output = bacon_functions.invert_actor_dict( {'cake': ['icing', 'cherry']}) correct_output = {'icing': ['cake'], 'cherry': ['cake']} assert program_output == correct_output,\ 'Single key multiple value list dictionary test'
def test_multiple_key_multiple_unique_values_dict(): '''Test with a dictionary containing multiple keys, with multiple distinct items in the list (value) which they map to. ''' D = { 'salad': ['lettuce','dressing','broccoli'], 'sandwich': [ 'ham', 'cheese'] } program_output = bacon_functions.invert_actor_dict(D) correct_output = { 'lettuce': ['salad'], 'dressing': ['salad'], 'broccoli': ['salad'], 'ham': ['sandwich'], 'cheese': ['sandwich'] } assert program_output == correct_output,\ 'Multiple key multiple (distinct) value list dictionary test'
def test_multiple_key_multiple_overlapping_values_dict(): '''Test with a dictionary containing multiple keys, with multiple overlapping items in the list (value) which they map to. ''' D = { 'modern warfare': ['brutal','entertaining','addictive'], 'angry birds': [ 'awesome', 'addictive'], 'counter-strike': ['brutal', 'awesome','addictive','entertaining'] } program_output = bacon_functions.invert_actor_dict(D) correct_output = { 'awesome': ['angry birds','counter-strike'], 'brutal': ['counter-strike','modern warfare'], 'addictive': ['modern warfare','angry birds','counter-strike'], 'entertaining': ['counter-strike','modern warfare'] } sort_dict_values(program_output) sort_dict_values(correct_output) assert program_output == correct_output,\ 'Multiple key multiple (overlapping) value list dictionary test'
def test_multiple_key_multiple_unique_values_dict(): '''Test with a dictionary containing multiple keys, with multiple distinct items in the list (value) which they map to. ''' D = { 'salad': ['lettuce', 'dressing', 'broccoli'], 'sandwich': ['ham', 'cheese'] } program_output = bacon_functions.invert_actor_dict(D) correct_output = { 'lettuce': ['salad'], 'dressing': ['salad'], 'broccoli': ['salad'], 'ham': ['sandwich'], 'cheese': ['sandwich'] } assert program_output == correct_output,\ 'Multiple key multiple (distinct) value list dictionary test'
def test_multiple_key_multiple_overlapping_values_dict(): '''Test with a dictionary containing multiple keys, with multiple overlapping items in the list (value) which they map to. ''' D = { 'modern warfare': ['brutal', 'entertaining', 'addictive'], 'angry birds': ['awesome', 'addictive'], 'counter-strike': ['brutal', 'awesome', 'addictive', 'entertaining'] } program_output = bacon_functions.invert_actor_dict(D) correct_output = { 'awesome': ['angry birds', 'counter-strike'], 'brutal': ['counter-strike', 'modern warfare'], 'addictive': ['modern warfare', 'angry birds', 'counter-strike'], 'entertaining': ['counter-strike', 'modern warfare'] } sort_dict_values(program_output) sort_dict_values(correct_output) assert program_output == correct_output,\ 'Multiple key multiple (overlapping) value list dictionary test'
''') expected = {"Kevin Bacon": ["A Few Good Men (1992)"], "Robert De Niro": ["Sleepers (1996)"]} result = bacon_functions.parse_actor_data(reader) if expected != result: print "Failure of function parse_actor_data: " + \ "Result should be %s, not %s" % (str(expected), str(result)) ok = False else: print "Function 'parse_actor_data' is not defined" ok = False if hasattr(bacon_functions, 'invert_actor_dict'): expected = {"A Few Good Men (1992)": ["Kevin Bacon"], "Sleepers (1996)": ["Kevin Bacon"]} result = bacon_functions.invert_actor_dict({"Kevin Bacon": ["A Few Good Men (1992)", "Sleepers (1996)"]}) if expected != result: print "Failure of function invert_actor_dict: " + \ "Result should be %s, not %s" % (str(expected), str(result)) ok = False else: print "Function 'invert_actor_dict' is not defined" ok = False if hasattr(bacon_functions, 'find_connection'): expected = [('Sleepers (1996)', 'Kevin Bacon')] result = bacon_functions.find_connection("Robert De Niro", {"Kevin Bacon": ["Sleepers (1996)"], "Robert De Niro": ["Sleepers (1996)"]}, {"Sleepers (1996)": ["Kevin Bacon", "Robert De Niro"]})
import bacon_functions if __name__ == '__main__': actor_data = open('large_actor_data.txt','r') # Changes the actor_data and actor_dict into dictionaries that we can use actor_dict = bacon_functions.parse_actor_data(actor_data) movie_dict = bacon_functions.invert_actor_dict(actor_dict) counter = 'continue' bacon_number_list = [] while counter == 'continue': # As long as counter is 'continue', while loop will continue running # Take the raw input and remove all unnecessary spaces actor_name = raw_input\ ('Please enter an actor (or press return to exit): ').strip() if actor_name != '': # If Kevin Bacon is not in the list and he is being searched, print # bacon number is infinity and gets rid of roman numerals if 'Kevin Bacon' == bacon_functions.fix_actor_name(actor_name) and\ 'Kevin Bacon' not in actor_dict: print 'Kevin Bacon has a Bacon Number of Infinity.' + '\n' # Exception: if the title of actor_name is 'Kevin Bacon', # return 0 as the Bacon Number. (gets rid of roman numerals) elif bacon_functions.fix_actor_name(actor_name) == 'Kevin Bacon': print bacon_functions.fix_actor_name(actor_name) + \ ' has a Bacon Number of 0.' + '\n'
} result = bacon_functions.parse_actor_data(reader) if expected != result: print "Failure of function parse_actor_data: " + \ "Result should be %s, not %s" % (str(expected), str(result)) ok = False else: print "Function 'parse_actor_data' is not defined" ok = False if hasattr(bacon_functions, 'invert_actor_dict'): expected = { "A Few Good Men (1992)": ["Kevin Bacon"], "Sleepers (1996)": ["Kevin Bacon"] } result = bacon_functions.invert_actor_dict( {"Kevin Bacon": ["A Few Good Men (1992)", "Sleepers (1996)"]}) if expected != result: print "Failure of function invert_actor_dict: " + \ "Result should be %s, not %s" % (str(expected), str(result)) ok = False else: print "Function 'invert_actor_dict' is not defined" ok = False if hasattr(bacon_functions, 'find_connection'): expected = [('Sleepers (1996)', 'Kevin Bacon')] result = bacon_functions.find_connection( "Robert De Niro", { "Kevin Bacon": ["Sleepers (1996)"], "Robert De Niro": ["Sleepers (1996)"] }, {"Sleepers (1996)": ["Kevin Bacon", "Robert De Niro"]})
import bacon_functions filename = open("large_actor_data.txt") actor_dict = bacon_functions.parse_actor_data(filename) movie_dict = bacon_functions.invert_actor_dict(actor_dict) c = 0 empty = [0] while c >= 0: actor_name = raw_input(\ "Please enter an actor (or press return to exit): ") actor_name = actor_name.title() if actor_name == "": # If an empty string was entred, we need to present the largest bacon # number that has been found. Because we collect all the bacon numbers # in a list, we just need to sort it and find the maxium number, which # is the last number. Then the whole game stopped. empty.sort() maxium = empty[-1] leave = "Thank you for playing! " + \ "The largest Bacon Number you found was " + str(maxium) print leave print '' c = -1 # -1 stopped the function from continuing.
# Parsing file of actors (male) imdb_actors_file = open("actors.list") print "Parsing file (actors.list) of actors (male) ..." actors_dict = bacon_functions.parse_actor_data(imdb_actors_file) imdb_actors_file.close() # Parsing file of actors (female) imdb_actresses_file = open("actresses.list") print "Parsing file (actresses.list) of actresses (female) ..." actresses_dict = bacon_functions.parse_actor_data(imdb_actresses_file) imdb_actresses_file.close() # thanks for the quick reminder: http://stackoverflow.com/a/38990 # Merging both the (actors to movies) data structures print "Merging both the (actors to movies) data structures ..." actors_to_movies = dict(actors_dict.items() + actresses_dict.items()) # Creating a movies to actors data structure print "Creating a movies to actors data structure ..." movies_to_actors = bacon_functions.invert_actor_dict(actors_to_movies) # Pickling both the data structures # Pickling the actors to movies data structure print "Pickling the actors to movies data structure ..." pickle_dump(actors_to_movies, "actors_to_movies") # Pickling the actors to movies data structure print "Pickling the actors to movies data structure ..." pickle_dump(movies_to_actors, "movies_to_actors") print "Done! Run bacon.py to begin playing!"
if __name__ == "__main__": # Parsing file of actors (male) imdb_actors_file = open("actors.list") print('Parsing file (actors.list) of actors (male) ...') actors_dict = bacon_functions.parse_actor_data(imdb_actors_file) imdb_actors_file.close() # Parsing file of actors (female) imdb_actresses_file = open("actresses.list") print('Parsing file (actresses.list) of actresses (female) ...') actresses_dict = bacon_functions.parse_actor_data(imdb_actresses_file) imdb_actresses_file.close() # thanks for the quick reminder: http://stackoverflow.com/a/38990 # Merging both the (actors to movies) data structures print('Merging both the (actors to movies) data structures ...') actors_to_movies = dict(actors_dict.items() + actresses_dict.items()) # Creating a movies to actors data structure print('Creating a movies to actors data structure ...') movies_to_actors = bacon_functions.invert_actor_dict(actors_to_movies) # Pickling both the data structures # Pickling the actors to movies data structure print('Pickling the actors to movies data structure ...') pickle_dump(actors_to_movies, 'actors_to_movies') # Pickling the actors to movies data structure print('Pickling the actors to movies data structure ...') pickle_dump(movies_to_actors, 'movies_to_actors') print('Done! Run bacon.py to begin playing!')