[Tutor] Re: first program

Andrei project5 at redrival.net
Sat Dec 6 07:07:46 EST 2003


RoT wrote on Sat, 06 Dec 2003 17:52:46 +0800:

> I wasn't aware of this. I found string.find in the 2.3 library reference so I didn't think it might
> be deprecated. Where are these new methods documented?

Paragraph 2.2.6.1 of the Library raference (Built-in objects -> Built-in
types -> Sequence types -> String methods).

It's deprecated at least for the part that is duplicated by the string
methods. But it still contains some constants which are in no other spot.

>> The change-option function 'chopt()' can be simplified by using a
>> "dispatch" table technique.  Here's a quick example to demonstrate the
>> idea:
>> 
>> ###
>> def add(x, y): return x + y
>> def sub(x, y): return x - y
>> dispatch_table = {'+' : add,
>>                   '-' : sub}
<snap>
> mmm nice work, I am going to have a play with this. I was thinking there must be a cleaner way of
> doing this but settled on this method to get the program up and running.

One of the nice things about Python is that you can put *anything* in
dictionaries, including functions :).

>> Last comment for now: the last main() method might be a bit long; you may
>> want to decompose it as you have the rest of the program.
> 
> I actually tried , about half way through the program, to break it up into modules, but after a
> couple of wasted hours of chasing  variable errors I gave up, not wanting to have a mass of globals
> everywhere. Next time I should think about modular code at the begining of the process :)

He's not talking about modules I think, but about the stuff you write at
the bottom, after the __name__=='__main__'. The program is at 500 lines not
too long to live in a single file. It's just that it's probably better to
add some more functions split that stuff up. E.g. instead of asking "We
have the ability to retreive 'n' numb... etc", you could make a function
StoreTextFile() and you could have function GetUserLogin which gets
password and the likes, etc. It makes it easier to follow the program flow.

Btw, my eye fell on "if "y" in ans:". I don't think that's a very good
test, e.g. if someone writes "no neverever nononono noway", it would be
interpreted as "yes", while the user would obviously expect a no. Given the
hint you give (y/n), it suggests that only the first letter is used. So I
think you should do: if ans.strip().lower()[0]=="y" to test for a positive
response: it's case insensitive and allows the user to use spaces first.
But depending on how safe you want it to be, you might prefer to test for a
no, so that anything that isn't a "n" (like a typo) defaults to the safe
answer "y".
It's interesting that a bit lower you use if raw_input("Logout and save
changes? y/n\n>>> ") == "y": to test for yes, which is a very different
method. 
You could actually make this into a function too, and add error checking in
that function to make sure the user only is allowed to type "y/yes" or
"n/no".

-- 
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list