Advise of programming one of my first programs

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon Apr 16 16:37:46 EDT 2012


> Ramit,
> 
> This seems to be more logic now "I hope" :)
> #########################################
> import ast
> fname = 0
> lname = 1
> country = 2
> city = 3
> tel = 4
> notes = 5
> 
> ## Read data from file
> 
> def load_book():
>     load_book = open('c:/Python27/Toli/myfile.txt', 'r')
>     load_book = ast.literal_eval(load_book.read())
>     return load_book
> 
> ## Write data to file
> 
> def write_book(tbook):
>     write_book = open('c:/Python27/Toli/myfile.txt', 'w')
>     write_book.write(repr(tbook))

Either use write_book.close() or a context manager instead. I suggest
the context manager approach but it only works on Python 2.6+

Otherwise, there is a chance that the file might not get written out.

> 
> ## Menu choice input
> 
> def get_menu_choice(text):
> 	choice = raw_input(text)
> 	return choice
> 
> ## List data contacts
> 
> def listpb():
>     ##tbook = load_book()
>     print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
>     print 'Nick\t\tF.Name\t\tL.Name\t\tCountry\t\t\tCity\t\tTel'
>     print '_' * 105,'\n','\t' * 13

tbook should be passed in. This is currently using the global variable
which is generally discouraged.

>     for val in tbook.keys():
>             print val, '\t\t', tbook[val][fname], '\t', tbook[val][lname],
> '\t', tbook[val][country], '\t\t', tbook[val][city], '\t\t',
> tbook[val][tel],'\t\t\n'
>     print '_'*105,'\n\n\n\n'


The following looks like menu choices and should instead be printed
along with the menu text.

>     print 'Type nickname and press <Enter> or type <Q> to exit.\n\n\n'
> 
> ## Main menu
> 
> def mmenu(tbook):
>     listpb()
>     while True:
>         text = 'Type your option: '
>         choice = get_menu_choice(text)
>         if choice == 'e' or choice == 'E':
>             text = 'Type nickname and press <Enter> to edit: '
>             choicen = get_menu_choice(text)
>             if choicen in tbook:
>                 edit(choicen, tbook)
>         elif choice == 'b' or choice == 'B':
>             listpb()
>         elif choice == 'd' or choice == 'D':
>             text = 'Type nickname and press <Enter> for details: '
>             choicen = get_menu_choice(text)
>             if choicen in tbook:
>                 details(choicen, tbook)
>         elif choice == 'q' or choice == 'Q':
>             break
>         else:
>             print 'Selection {0} not understood.'.format(choice)
> 
> ## Contact details
> 
> def details(choicen, tbook):
>     sb = tbook[choicen]
>     print 'Nickname: ', choicen, ' is selected\n'
>     print 'First name:\t', sb[fname], '\n'
>     print 'Last name:\t', sb[lname], '\n'
>     print 'Country:\t', sb[country], '\n'
>     print 'City:\t\t', sb[city], '\n'
>     print 'Phone number:\t',sb[tel], '\n'
>     print 'Memos:\n'
>     print sb[notes]

Again, the following menu options should be printed where you 
call get_menu_choice.

>     print '\n\n(E)dit\n\n'
>     print '(B)ack to phonebook list\n\n'
> 
> ## Edit contact
> 
> def edit(choicen, tbook):
>     sb = tbook[choicen]
>     fn = raw_input('New name for ' + sb[fname] + ' : ')
>     if fn == '':
>         pass
>     else:
>         sb[fname] = fn
>     ln = raw_input('New name for ' + sb[lname] + ' : ')
>     if ln == '':
>         pass
>     else:
>         sb[lname] = ln
>     write_book(tbook)
>     details(choicen, tbook)
> tbook = load_book()
> mmenu(tbook)
> 
> #######################################
> 
> What you thing?
> 
> Regards
> 
> Anatoli

Looks better. Instead of having global variables (fname, lname, 
tel) why not just use a string in 'last_name', 'first_name', and 
'telephone #'? That would be more self explanatory when looking at 
the data file and reading the code.

References like 
    sb[lname] 
would become 
    sb['last_name']

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Python-list mailing list