#!/Users/justin/Python/bin/python # An intereactive rolodex import string, sys, pickle, cmd class Rolodex(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) # iniitalize the base class self.prompt = "Justin's Friends: " # Customize the prompt self.people = {} # At first, we know noone def help_add(self): print "Adds an entry (specify a name)" def do_add(self, name): if name == "": name = raw_input ("Enter name: ") phone = raw_input("Enter phone numer for "+ name+" ") self.people[name] = phone # Add phone number for name def help_find(self): print "Find an entry (specify a name)" def do_find(self, name): if name == "": name = raw_input ("Enter name: ") if self.people.has_key(name): print "The number for %s is %s." % (name, self.people[name]) else: print "We have no record for %s" % (name,) def help_list(self): print "Prints the contents of the directory" def do_list(self, line): names = self.people.keys() # the keys are the names if names == []: return # if there are no names, exit names.sort() # we want them in alphabetic order for name in names: print string.rjust(name, 20), ":", string.ljust(self.people[name], 20) print '='*41 def help_EOF(self): print "Quits the program" def do_EOF(self, line): print "OK, I'm leaving" sys.exit() def help_save(self): print "Save the current state of affairs" def do_save(self, filename): if filename == "": filename = raw_input("Enter filename: ") saveFile = open(filename, 'w') self.people = pickle.dump(self.people, saveFile) def help_load(self): print "Load a directory" def do_load(self, filename): if filename == "": filename = raw_input("Enter filename: ") saveFile = open(filename, 'r') self.people = pickle.load(saveFile) # note that this will override # any existing people directory print "We're running as ", __name__ if __name__ == '__main__': # this way the module can be print "Running away..." rolo = Rolodex() # imported by other programs as well rolo.cmdloop() else: print "Captive!"