import os.path import time import cPickle def get_file(): '''Prompt for the name of an existing file, open the file for reading, and return the file object.''' filename = raw_input("Please enter a filename: ") while not os.path.exists(filename): print filename, "does not exist" filename = raw_input("Please enter a filename: ") return open(filename,'r') def get_line(myfile,keep_capital_letters): '''Read a line from open file myfile and return it, but with the new line character at the end removed.''' line = myfile.readline() if keep_capital_letters: return line[:-1] else: return line[:-1].lower() def get_input(string,choices): ans = raw_input(string) while not ans in choices: print "Invalid response." ans = raw_input(string) return ans def create_dicts(pfile): def get_items(dict,pfile,cur_pic): # we know there is at least one line in these sections line = get_line(pfile,False) while not line == "end": if line in dict: dict[line].append(cur_pic) else: dict[line] = [cur_pic] line = get_line(pfile,False) new_picture = True pictures = [] faces = {} places = {} events = {} st2dict = {"faces":faces,"places":places,"events":events} line = get_line(pfile,new_picture) while line != "": if new_picture: pictures.append(line) new_picture = False cur_pic = line elif line == "endpic": new_picture = True # line is 'faces', 'places', or 'events' else: get_items(st2dict[line],pfile,cur_pic) line = get_line(pfile,new_picture) pfile.close() return (pictures,faces,places,events) def filter(pictures,faces,places,events): def include_or_exclude(iore,dict,whichone,pictures): if iore == 'i': # add the chosen pictures, but only if they are not already there for i in dict[whichone]: if not i in pictures: pictures.append(i) else: # exclude all photos in the pictures list that match copy_pictures = pictures[:] for i in copy_pictures: if i in dict[whichone]: pictures.remove(i) ans = get_input("Would you like load in a current picture list? (y/n)? ",['y','n']) if ans == 'y': pf = get_file() cur_picture_list = cPickle.load(pf) pf.close() else: cur_picture_list = [] st2dict = {"f":faces,"p":places,"e":events} choice2question = {"f":"whose face","p":"which place","e":"what event"} choice2pronoun = {"f":"them","p":"it","e":"it"} print "There are",len(cur_picture_list), "pictures .",cur_picture_list notdone = get_input( "Would you like to filter the list (y/n)? ",['y','n']) while notdone == 'y': whichtype = get_input("Based on faces, places or events (f/p/e)? ",['f','p','e']) whichone = get_input("Based on " + choice2question[whichtype] + "? ", st2dict[whichtype].keys()) iore = get_input("Include or exclude " + choice2pronoun[whichtype] + "(i/e)? ",['i','e']) include_or_exclude(iore,st2dict[whichtype],whichone,cur_picture_list) print "There are",len(cur_picture_list), "pictures.",cur_picture_list notdone = get_input( "Would you like to filter the list (y/n)? ",['y','n']) return cur_picture_list def give_slide_show(pictures): s = int(raw_input("How many seconds would you like to view each picture? ")) for p in pictures: pic = media.load_picture(p) media.show(pic) time.sleep(s) media.close(pic) def print_pictures(pictures): print "Your picture list is:" for p in pictures[:-1]: print "%s; " % (p), print "%s" % pictures[-1] if __name__ == '__main__': pfile = get_file() (pictures,faces,places,events) = create_dicts(pfile) new_pictures = filter(pictures,faces,places,events) print_pictures(new_pictures) ans = get_input("Would you like to see a slide show? (y/n)? ",['y','n']) if ans == 'y': give_slide_show(new_pictures) ans = get_input("Would you like to save the current picture list? (y/n)? ",['y','n']) if ans == 'y': ans = raw_input("Enter a file name. ") f = open(ans,'w') cPickle.dump(new_pictures,f) f.close()