Kent Johnson <kent37 <at> tds.net> writes: [snip] > You still aren't doing anything with newdic. The absence of 'newdic' in the > code > after 'read.close()' should be a clue > > I think you want to overwrite the saved dict, but with the new dict instead > of > with a filename string...
Hi Kent, First off, thanks again to you and Jacob and all. I have updated the simple address book program and showing it here. To me this program seems working to my expectation. but i need good advise from the knowledgeable people here how to improve the program, comment on the coding style, syntax and other things i obviously miss being a newbie. I know this is not much of a program sans orinality as others must have been done this a thousand times much better than mine. but anyway this is good exercise for me to improve my understanding of python. TIA. And sorry for this long boring code :-)). I hope the indentation stays intact. #!/usr/bin/env python ''' CHANGELOG Tue Jan 25 08:11:07 AST 2005 1. moved all globals under if __name__ == '__main__' Mon Jan 24 13:12:39 AST 2005 1. wrap few more common codes with function call Sun Jan 23 15:06:54 AST 2005 1. address book is complete(at least to me) :-)) ''' ## Functions ################################################################## def about_program(): try: print print '\t'*2, '%s - %s' % (_name_, _version_) print '\t'*2, 'A simple address book written in Python' print '\t'*2, 'Cebwrpg Pbyynobengvba bs: ' print '\t' *2, _authors_[0], ',', _authors_[1] print '\t'*2, 'Created:', _created_ print '\t'*2, 'Revised:', _revised_ raw_input('\nPress <Enter> to continue...') clear_screen() except EOFError: print ## add_contact ## def add_contact(d, f): while True: name = add_name() email = add_email() d[name] = email print 'Add another contact? ' ans = ask_yes_no() if ans == 0: # no print 'Save to address book? ' get_ans = ask_yes_no() if get_ans == 1: # yes save check = if_file_exist(f) if check == True: read = open(f, 'rb') stored = cPickle.load(read) stored.update(d) print "'%s' added to address book!\n" % d[name] read.close() write_book(stored, filename) clear_screen() else: # file not exist write_book(data_holder, filename) break else: # no save d.clear() #clear dict cache clear_screen() break def add_name(): msg = _msg_ + ' (add): ' while True: try: name = raw_input(msg) if len(name) != 0: if len(name) <= 20: return name else: print 'Name too long: please limit to 20 characters' else: print 'Try again: blank not allowed!' except EOFError: # catch ^C-D keypress print def add_email(): msg = 'Enter email address: ' while True: try: email = raw_input(msg) if len(email) == 0: print 'Blank not allowed!' else: valid_format = r'[EMAIL PROTECTED](\.[-a-z0-9]+)*\.(com$|\ edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|museum$|pro$|info$)' valid_email = re.compile(valid_format) if valid_email.match(email): return email else: print '%s is not a valid address: try again!' % email except EOFError: print def ask_yes_no(): ask = raw_input('Yes or No? (y|[N]) ') if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']: return 1 # yes else: return 0 # no def if_file_exist(f): ''' test if file exists; returns boolean ''' return os.path.exists(os.path.join(os.path.expanduser('~'), f)) def write_book(d, f): write = open(f, 'wb') cPickle.dump(d, write) write.close() def update_dict(d, f): ''' update the saved dictionary file ''' read = open(f, 'rb') newdic = cPickle.load(read) newdic.update(d) read.close() def view_abook(d, f): check = if_file_exist(f) if check is True: display_contacts(data_holder, filename) raw_input('\nPress <Enter> to continue...') clear_screen() else: print 'no contacts listed!' def display_contacts(d, f): read = open(f, 'rb') d = cPickle.load(read) print 'Total contact[s]: %d\n' % len(d) while 1: index = 0 for key in d: name = key while len(name) < 25: name += '.' # append to name index += 1 print index, name, d[key] break read.close() def find_contact(d, f): msg = _msg_ + ' (find): ' while 1: try: read = open(f, 'rb') d = cPickle.load(read) find = raw_input(msg) if d.has_key(find): print 'Found:%s\temail:%s' % (find, d[find]) else: print "'%s' not found" % find print 'Try again? (find)' ans = ask_yes_no() if ans == 0: #no read.close() clear_screen() break read.close() except EOFError: print def remove_contact(d, f): msg = _msg_ + ' (remove): ' while 1: try: read = open(f, 'rb') stored = cPickle.load(read) # assign new dict name remove = raw_input(msg) if stored.has_key(remove): print "Found '%s'" % remove print "Really remove '%s'?" % remove confirm = ask_yes_no() if confirm == 1: #yes del stored[remove] stored.update(d) # update changed dict read.close() print "'%s' is removed!" % remove write_book(stored, filename) else: read.close() else: print "'%s' not found" % remove print 'Try again? (remove)' ans = ask_yes_no() if ans == 0: read.close() clear_screen() break except EOFError: print def edit_contact(d, f): msg = _msg_ + ' (edit): ' while 1: try: read = open(f, 'rb') stored = cPickle.load(read) edit = raw_input(msg) if stored.has_key(edit): print "Found '%s'" % edit print "Really edit '%s'?" % edit confirm = ask_yes_no() if confirm == 1: # yes print "'%s' with email: %s will be edited" \ % (edit, stored.pop(edit)) name = add_name() email = add_email() stored[name] = email # add new name/email stored.update(d) read.close() write_book(stored, filename) else: # no, dont edit read.close() else: print "'%s' not found!" % edit print 'Try again? (edit)' ans = ask_yes_no() if ans == 0: # no read.close() clear_screen() break except EOFError: print def clear_screen(): os.system('clear') def ifaccessible(f): ''' test if file is accessible by user; returns boolean ''' return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK) def main(): while True: select = main_menu() while True: if select in [ '1', 'p']: about_program() break elif select in [ '2', 'a']: add_contact(data_holder, filename) break elif select in [ '3', 'v']: view_abook(data_holder, filename) break elif select in [ '4', 'f']: find_contact(data_holder, filename) break elif select in [ '5', 'e']: edit_contact(data_holder, filename) break elif select in [ '6', 'r']: remove_contact(data_holder, filename) break elif select in [ '7', 'q']: sys.exit('Goodbye') else: print "'%s' is invalid option!" % select break def main_menu(): '''Show menu options''' print """ MENU OPTIONS [1] About This [P]rogram [2] [A]dd Contact [3] [V]iew Address Book [4] [F]ind Contact [5] [E]dit Contact [6] [R]emove Contact [7] [Q]uit """ while 1: try: ask = '\nSelect an option: ' opt = raw_input(ask)[0] #if opt.isalpha() == True: # truth test redundant if opt.isalpha(): return opt.lower() return opt except (IndexError, EOFError): # catch <Enter>, <^C-D> keys pass ## main program portion ## if __name__ == '__main__': import cPickle, os, sys, string, re #globals _name_ = 'Py_AddBook' _version_ = '1.00' _created_ = 'December 2, 2004' _revised_ = 'Sun Jan 23 15:06:54 AST 2005' _authors_ = ['Rev Zraqm <[EMAIL PROTECTED]>', 'Whfgva Fgenhor <[EMAIL PROTECTED]>'] _msg_ = 'Enter contact name' home = '~' filename = os.path.join(os.path.expanduser(home), 'abook.dat') data_holder = {} main() _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor