Advise of programming one of my first programs

Anatoli Hristov tolidtm at gmail.com
Tue Mar 27 16:55:32 CEST 2012


Thank you Ramit for your advice`s. I`m reading a book ( Learning Python,
Second Edition ) by Mark Lutz and David Ascher and now I just finished the
Basic Function lesson :) I will keep in mind what you have advised me, but
will implement it later when I have more experience with the book, because
I don`t understand exactly what you mean by doing all dose changes :)
By the way I dont know why your mail was in my junk I just saw it.

And here is my last code I did for the phonebook:
Thanks

####### CODE #########

fileread = open('myfile.txt','r')
tbook = eval(fileread.read())
fileread.close()


## Edit selected nickname
def edit():
    sb = tbook[select]
    fn = raw_input('New name for ' + sb[0] + ' : ')
    sb[0] = fn
    ln = raw_input('New name for ' + sb[1] + ' : ')
    sb[1] = ln
    filewrite = open('myfile.txt','w')
    filewrite.write(str(tbook))
    filewrite.close()
    raw_input('\n\n\nPress <Enter> to return')
    details()


## Details of nickname
def details():
        sb = tbook[select]
        print 'Nickname: ', select, ' is selected\n'
        print 'First name:\t', sb[0], '\n'
        print 'Last name:\t', sb[1], '\n'
        print 'Country:\t', sb[2], '\n'
        print 'City:\t\t', sb[3], '\n'
        print 'Phone number:\t',sb[4], '\n'
        print 'Memos:\n'
        print sb[5]

        print '\n\n(E)dit\n\n'
        print '(B)ack to phonebook list\n\n'
        menu = raw_input('What you wana do? ')
        if menu == 'e' or 'E':
                edit()
        if menu == 'b' or 'B':
            listpb()


## Select nickname
def selectm():
    global select
    select = raw_input('Type nickname and press <Enter>: ')
    if select == '':
        listpb()
    if select in tbook:
        details()
    else:
        listpb()

## List all contacts
def listpb():
    print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'

    print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
    print '_' * 105,'\n','\t' * 13
    for val in tbook.keys():
            print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
    print '_'*105,'\n\n'
    selectm()

## Call list names
listpb()





On Tue, Mar 27, 2012 at 12:40 AM, Prasad, Ramit
<ramit.prasad at jpmorgan.com>wrote:

>
> > Hi guys just wanted to share one of my first programs. Could you please
> > tell me, do I use a right logic ?
> > It works fine what I wanted to do, but is it writen in the right way? My
> > next step is to make it write the changes of the dictionary on the file
> :)
> >
>
> When you do get that far, you should look at the pickle library.
> It is amazing how easy it is to store data with Python.
>
> >
> > ## DB
> > tbook = {'goodie':['Christian','Van Eckel','Bruxelles','Forest','02 344
> 33
> > 33','This is a test note :)'],
> >          'osvaldo':['Osvaldo','Rios','Liege','Centrum','023758832',''],
> >          'ronaldo':['Diego','Aspanda','Brussels','Vorst','03 443 23
> > 23','']}
> >
> > ## Edit selected nickname
> > def edit():
> >     sb = tbook[select]
> >     fn = raw_input('New name for ' + sb[0] + ' : ')
> >     sb[0] = fn
> >     ln = raw_input('New name for ' + sb[1] + ' : ')
> >     sb[1] = ln
> >     raw_input('\n\n\nPress <Enter> to return')
> >     details()
> >
> >
> > ## Details of nickname
> > def details():
> >         sb = tbook[select]
> >         print 'Nickname: ', select, ' is selected\n'
> >         print 'First name:\t', sb[0], '\n'
> >         print 'Last name:\t', sb[1], '\n'
> >         print 'Country:\t', sb[2], '\n'
> >         print 'City:\t\t', sb[3], '\n'
> >         print 'Phone number:\t',sb[4], '\n'
> >         print 'Memos:\n'
> >         print sb[5]
> >
> >         print '\n\n(E)dit\n\n'
> >         print '(B)ack to phonebook list\n\n'
> >         menu = raw_input('What you wana do? ')
> >         if menu == 'e':
> >                 edit()
> >         if menu == 'b':
> >             listpb()
> >
>
> Minor nitpick, but what if the user types 'B' or 'E' like in
> your printed menu?
>
> >
> > ## Select nickname
> > def selectm():
> >     global select
> >     select = raw_input('Type nickname and press <Enter>: ')
> >     if select == '':
> >         listpb()
> >     if select in tbook:
> >         details()
> >     else:
> >         listpb()
>
>
> Remove all global variables when your program starts to work.
> Instead pass them as arguments and return them from functions.
> So do 'details( select )' instead of 'details()' and then in
> details, you would do edit( select ).
> >
> > ## List all contacts
> > def listpb():
> >     print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
> >
> >     print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
> >     print '_' * 105,'\n','\t' * 13
> >     for val in tbook.keys():
> >             print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
> > tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
> >     print '_'*105,'\n\n'
> >     selectm()
> >
> > ## Call list names
> > listpb()
>
> if __name__ == "__main__":
>    listpb()
>
> This way you can import the module and not run it on import; it is
> useful when you start wanting to reuse functions from a different
> project. It is better than copy-pasting functions everywhere because
> when you improve the function all the programs will pick it up. Otherwise
> you will have to go back to each pasted function and pick it up.
>
>
> A few items I would work to improve:
> 1. Remove global variables (already mentioned above)
>
> 2. You should separate any menu / navigation from your application
> code. details() should only print the details and not take the
> next menu choice. You probably want 2 separate menu functions.
> One that returns a 'select'-ed book and one that returns next choice.
> This will also help you when you implement my next point.
>
> 3. You should also change the structure of your program to loop
> instead of calling listpb each time you want to restart. It is a
> much better practice and while it will not affect this program
> unless you do not exit for 10s or 100s of thousands of details but if
> you write something that *does* navigate that many times it can crash.
> Looping is probably your next programming lesson anyway :)
>
> 4. This is more of a side note but instead of using \t\t all the
> time, you would be better off  learning to use the string formatting
> operations. It is a little more effort to learn, but tends to be
> a lot more reliable on  different systems (and with different
> data trying to be printed) than manually trying to align everything.
>
> http://docs.python.org/library/string.html#format-string-syntax
>
>
> Keep on working, you have made a good start and now it is time
> to refactor (programming equivalent of rewriting an essay) and
> make everything better!
>
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120327/7b44a46c/attachment.html>


More information about the Python-list mailing list