Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread Wolfram Kraus
You wrote filename == raw_input("Filename to load: ") instead of 
filename = raw_input("Filename to load: ")

HTH,
Wolfram

Nathan Pinno wrote:
> Hi all,
> 
> Here's one of the messages that pops up:
> 
> Traceback (most recent call last):
>   File "D:\password.py", line 77, in ?
> filename == raw_input("Filename to load: ")
> NameError: name 'filename' is not defined
> 
> Why is it popping up whenever I try to load a file?
> 
> Here's the latest code:
> 
> # This is the code for a password protected program to store passwords.
> password = "hello"
> print "The Password Program"
> print "Copyright 2005 Nathan Pinno."
> print
> answer = raw_input("What is the password? ")
> while password != answer:
> print "The password is incorrect."
> answer = raw_input("What is the password? ")
> def main_menu():
> print "1) Add a login info card"
> print "2) Lookup a login info card"
> print "3) Remove a login info card"
> print "4) Print Login info list"
> print "5) Save login list"
> print "6) Open Login list"
> print "9) Exit"
> 
> def load_login(site,filename):
> in_file = open(filename,"r")
> while 1:
> in_line = in_file.readline()
> if len(in_file) == 0:
> break
> in_line = in_line[:-1]
> [site,id,passcard] = string.split(in_line,",")
> list[site] = id and passcard
> in_file.close()
> 
> def save_login(site,filename):
> out_file = open(filename,"w")
> for x in site.keys():
> out_file.write(x+","+sites[x]+"\n")
> out_file.close()
> 
> menu_choice = "0"
> list = {}
> print "Welcome to the second half of the program."
> print main_menu()
> while menu_choice != "9":
> menu_choice = raw_input("Choose an option: ")
> if menu_choice == "1":
> print "Add a login info card"
> site = raw_input("Site: ")
> id = raw_input("User ID: ")
> passcard = raw_input("Password: ")
> list[site] = id and passcard
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "2":
> print "Lookup a login info card"
> site = raw_input("Site: ")
> if site.has_key(site):
> print "The ID is: ",id(site)
> print "The password is: ",passcard(site)
> else:
> print site," was not found."
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "3":
> print "Remove a login info card"
> site = raw_input("Site: ")
> if sites.has_key(site):
> del numbers[site]
> else:
> print site," was not found."
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "4":
> print "Login Info"
> for x in site.keys():
> print "Site: ",x," \tID: ",numbers[x]," \tPassword: ",numbers[x]
> print
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "5":
> filename = raw_input("Filename to save: ")
> save_login(list,filename)
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "6":
> filename == raw_input("Filename to load: ")
> load_login(list,filename)
> menu_choice = raw_input("Choose an option: ")
> print "Have a nice day!"
> 
> Anything else that needs addressing?
> 
> Thanks,
> Nathan Pinno
> http://www.npinnowebsite.ca/
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread Nathan Pinno
  Thanks Wolfram for help with that error.

  Here's another that popped up:

  Traceback (most recent call last):
File "D:\password.py", line 68, in ?
  for x in site.keys():
  AttributeError: 'str' object has no attribute 'keys'

  How to fix it?

  Thanks,
  Nathan Pinno
  - Original Message - 
  From: "Wolfram Kraus" <[EMAIL PROTECTED]>
  To: 
  Sent: Thursday, July 07, 2005 1:02 AM
  Subject: Re: [Tutor] Why is this error showing up? (Original Message: 
(Tutor) What's wrong with this code?) Ignore previous post.


  > You wrote filename == raw_input("Filename to load: ") instead of
  > filename = raw_input("Filename to load: ")
  >
  > HTH,
  > Wolfram
  >
  > Nathan Pinno wrote:
  >> Hi all,
  >>
  >> Here's one of the messages that pops up:
  >>
  >> Traceback (most recent call last):
  >>   File "D:\password.py", line 77, in ?
  >> filename == raw_input("Filename to load: ")
  >> NameError: name 'filename' is not defined
  >>
  >> Why is it popping up whenever I try to load a file?
  >>
  >> Here's the latest code:
  >>
  >> # This is the code for a password protected program to store passwords.
  >> password = "hello"
  >> print "The Password Program"
  >> print "Copyright 2005 Nathan Pinno."
  >> print
  >> answer = raw_input("What is the password? ")
  >> while password != answer:
  >> print "The password is incorrect."
  >> answer = raw_input("What is the password? ")
  >> def main_menu():
  >> print "1) Add a login info card"
  >> print "2) Lookup a login info card"
  >> print "3) Remove a login info card"
  >> print "4) Print Login info list"
  >> print "5) Save login list"
  >> print "6) Open Login list"
  >> print "9) Exit"
  >>
  >> def load_login(site,filename):
  >> in_file = open(filename,"r")
  >> while 1:
  >> in_line = in_file.readline()
  >> if len(in_file) == 0:
  >> break
  >> in_line = in_line[:-1]
  >> [site,id,passcard] = string.split(in_line,",")
  >> list[site] = id and passcard
  >> in_file.close()
  >>
  >> def save_login(site,filename):
  >> out_file = open(filename,"w")
  >> for x in site.keys():
  >> out_file.write(x+","+sites[x]+"\n")
  >> out_file.close()
  >>
  >> menu_choice = "0"
  >> list = {}
  >> print "Welcome to the second half of the program."
  >> print main_menu()
  >> while menu_choice != "9":
  >> menu_choice = raw_input("Choose an option: ")
  >> if menu_choice == "1":
  >> print "Add a login info card"
  >> site = raw_input("Site: ")
  >> id = raw_input("User ID: ")
  >> passcard = raw_input("Password: ")
  >> list[site] = id and passcard
  >> menu_choice = raw_input("Choose an option: ")
  >> elif menu_choice == "2":
  >> print "Lookup a login info card"
  >> site = raw_input("Site: ")
  >> if site.has_key(site):
  >> print "The ID is: ",id(site)
  >> print "The password is: ",passcard(site)
  >> else:
  >> print site," was not found."
  >> menu_choice = raw_input("Choose an option: ")
  >> elif menu_choice == "3":
  >> print "Remove a login info card"
  >> site = raw_input("Site: ")
  >> if sites.has_key(site):
  >> del numbers[site]
  >> else:
  >> print site," was not found."
  >> menu_choice = raw_input("Choose an option: ")
  >> elif menu_choice == "4":
  >> print "Login Info"
  >> for x in site.keys():
  >> print "Site: ",x," \tID: ",numbers[x]," \tPassword: 
",numbers[x]
  >> print
  >> menu_choice = raw_input("Choose an option: ")
  >> elif menu_choice == "5":
  >> filename = raw_input("Filename to save: ")
  >> save_login(list,filename)
  >> menu_choice = raw_input("Choose an option: ")
  >> elif menu_choice == "6":
  >> filename == raw_input("Filename to load: ")
  >> load_login(list,filename)
  >> menu_choice = raw_input("Choose an option: ")
  >> print "Have a nice day!"
  >>
  >> Anything else that needs addressing?
  >>
  >> Thanks,
  >> Nathan Pinno
  >> http://www.npinnowebsite.ca/
  >>
  >
  > ___
  > Tutor maillist  -  Tutor@python.org
  > http://mail.python.org/mailman/listinfo/tutor
  >
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread Ewald Ertl
Hi!


on Thu, 7 Jul 2005 01:13:48 -0600  "Nathan Pinno" <[EMAIL PROTECTED]> wrote :
-

Nathan Pinno >   Thanks Wolfram for help with that error.
Nathan Pinno > 
Nathan Pinno >   Here's another that popped up:
Nathan Pinno > 
Nathan Pinno >   Traceback (most recent call last):
Nathan Pinno > File "D:\password.py", line 68, in ?
Nathan Pinno >   for x in site.keys():
Nathan Pinno >   AttributeError: 'str' object has no attribute 'keys'
Nathan Pinno > 

The Traceback tells you on which line the error is. 
site comes from your input : site = raw_input("Site: ")
which is a string and not a dictionary.



Nathan Pinno >   How to fix it?
Nathan Pinno > 
Nathan Pinno >   Thanks,
Nathan Pinno >   Nathan Pinno
Nathan Pinno >   - Original Message - 
Nathan Pinno >   From: "Wolfram Kraus" <[EMAIL PROTECTED]>
Nathan Pinno >   To: 
Nathan Pinno >   Sent: Thursday, July 07, 2005 1:02 AM
Nathan Pinno >   Subject: Re: [Tutor] Why is this error showing up? (Original 
Message: 
Nathan Pinno > (Tutor) What's wrong with this code?) Ignore previous post.
Nathan Pinno > 
Nathan Pinno > 
Nathan Pinno >   > You wrote filename == raw_input("Filename to load: ") 
instead of
Nathan Pinno >   > filename = raw_input("Filename to load: ")
Nathan Pinno >   >
Nathan Pinno >   > HTH,
Nathan Pinno >   > Wolfram
Nathan Pinno >   >
Nathan Pinno >   > Nathan Pinno wrote:
Nathan Pinno >   >> Hi all,
Nathan Pinno >   >>
Nathan Pinno >   >> Here's one of the messages that pops up:
Nathan Pinno >   >>
Nathan Pinno >   >> Traceback (most recent call last):
Nathan Pinno >   >>   File "D:\password.py", line 77, in ?
Nathan Pinno >   >> filename == raw_input("Filename to load: ")
Nathan Pinno >   >> NameError: name 'filename' is not defined
Nathan Pinno >   >>
Nathan Pinno >   >> Why is it popping up whenever I try to load a file?
Nathan Pinno >   >>
Nathan Pinno >   >> Here's the latest code:
Nathan Pinno >   >>
Nathan Pinno >   >> # This is the code for a password protected program to 
store passwords.
Nathan Pinno >   >> password = "hello"
Nathan Pinno >   >> print "The Password Program"
Nathan Pinno >   >> print "Copyright 2005 Nathan Pinno."
Nathan Pinno >   >> print
Nathan Pinno >   >> answer = raw_input("What is the password? ")
Nathan Pinno >   >> while password != answer:
Nathan Pinno >   >> print "The password is incorrect."
Nathan Pinno >   >> answer = raw_input("What is the password? ")
Nathan Pinno >   >> def main_menu():
Nathan Pinno >   >> print "1) Add a login info card"
Nathan Pinno >   >> print "2) Lookup a login info card"
Nathan Pinno >   >> print "3) Remove a login info card"
Nathan Pinno >   >> print "4) Print Login info list"
Nathan Pinno >   >> print "5) Save login list"
Nathan Pinno >   >> print "6) Open Login list"
Nathan Pinno >   >> print "9) Exit"
Nathan Pinno >   >>
Nathan Pinno >   >> def load_login(site,filename):
Nathan Pinno >   >> in_file = open(filename,"r")
Nathan Pinno >   >> while 1:
Nathan Pinno >   >> in_line = in_file.readline()
Nathan Pinno >   >> if len(in_file) == 0:
Nathan Pinno >   >> break
Nathan Pinno >   >> in_line = in_line[:-1]
Nathan Pinno >   >> [site,id,passcard] = string.split(in_line,",")
Nathan Pinno >   >> list[site] = id and passcard
Nathan Pinno >   >> in_file.close()
Nathan Pinno >   >>
Nathan Pinno >   >> def save_login(site,filename):
Nathan Pinno >   >> out_file = open(filename,"w")
Nathan Pinno >   >> for x in site.keys():

site comes form your input :  site = raw_input("Site: ")
and is a string and not a dictionary. 
Perhaps you will use here "sites", but there are no items added to 
sites??


Nathan Pinno >   >> out_file.write(x+","+sites[x]+"\n")
Nathan Pinno >   >> out_file.close()
Nathan Pinno >   >>
Nathan Pinno >   >> menu_choice = "0"
Nathan Pinno >   >> list = {}
Nathan Pinno >   >> print "Welcome to the second half of the program."
Nathan Pinno >   >> print main_menu()
Nathan Pinno >   >> while menu_choice != "9":
Nathan Pinno >   >> menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >> if menu_choice == "1":
Nathan Pinno >   >> print "Add a login info card"
Nathan Pinno >   >> site = raw_input("Site: ")
Nathan Pinno >   >> id = raw_input("User ID: ")
Nathan Pinno >   >> passcard = raw_input("Password: ")
Nathan Pinno >   >> list[site] = id and passcard
Nathan Pinno >   >> menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >> elif menu_choice == "2":
Nathan Pinno >   >> print "Lookup a login info card"
Nathan Pinno >   >> site = raw_input("Site: ")
Nathan Pinno >   >> if site.has_key(site):
Nathan Pinno >   >> print "The ID is: ",id(site)
Nathan Pinno >   >> print "The password is: ",passcard(site)
Nathan Pinno 

Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread Nathan Pinno
  How do I change it to a dictionary, then? Or do I have to delete it, and 
just code it in the main part of the code?
  - Original Message - 
  From: "Ewald Ertl" <[EMAIL PROTECTED]>
  To: 
  Sent: Thursday, July 07, 2005 1:36 AM
  Subject: Re: [Tutor] Why is this error showing up? (Original Message: 
(Tutor) What's wrong with this code?) Ignore previous post.


  > Hi!
  >
  >
  > on Thu, 7 Jul 2005 01:13:48 -0600  "Nathan Pinno" 
<[EMAIL PROTECTED]> wrote :
  > 
-
  >
  > Nathan Pinno >   Thanks Wolfram for help with that error.
  > Nathan Pinno >
  > Nathan Pinno >   Here's another that popped up:
  > Nathan Pinno >
  > Nathan Pinno >   Traceback (most recent call last):
  > Nathan Pinno > File "D:\password.py", line 68, in ?
  > Nathan Pinno >   for x in site.keys():
  > Nathan Pinno >   AttributeError: 'str' object has no attribute 'keys'
  > Nathan Pinno >
  >
  > The Traceback tells you on which line the error is.
  > site comes from your input : site = raw_input("Site: ")
  > which is a string and not a dictionary.
  >
  >
  >
  > Nathan Pinno >   How to fix it?
  > Nathan Pinno >
  > Nathan Pinno >   Thanks,
  > Nathan Pinno >   Nathan Pinno
  > Nathan Pinno >   - Original Message - 
  > Nathan Pinno >   From: "Wolfram Kraus" <[EMAIL PROTECTED]>
  > Nathan Pinno >   To: 
  > Nathan Pinno >   Sent: Thursday, July 07, 2005 1:02 AM
  > Nathan Pinno >   Subject: Re: [Tutor] Why is this error showing up? 
(Original Message:
  > Nathan Pinno > (Tutor) What's wrong with this code?) Ignore previous 
post.
  > Nathan Pinno >
  > Nathan Pinno >
  > Nathan Pinno >   > You wrote filename == raw_input("Filename to load: ") 
instead of
  > Nathan Pinno >   > filename = raw_input("Filename to load: ")
  > Nathan Pinno >   >
  > Nathan Pinno >   > HTH,
  > Nathan Pinno >   > Wolfram
  > Nathan Pinno >   >
  > Nathan Pinno >   > Nathan Pinno wrote:
  > Nathan Pinno >   >> Hi all,
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> Here's one of the messages that pops up:
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> Traceback (most recent call last):
  > Nathan Pinno >   >>   File "D:\password.py", line 77, in ?
  > Nathan Pinno >   >> filename == raw_input("Filename to load: ")
  > Nathan Pinno >   >> NameError: name 'filename' is not defined
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> Why is it popping up whenever I try to load a file?
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> Here's the latest code:
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> # This is the code for a password protected program 
to store passwords.
  > Nathan Pinno >   >> password = "hello"
  > Nathan Pinno >   >> print "The Password Program"
  > Nathan Pinno >   >> print "Copyright 2005 Nathan Pinno."
  > Nathan Pinno >   >> print
  > Nathan Pinno >   >> answer = raw_input("What is the password? ")
  > Nathan Pinno >   >> while password != answer:
  > Nathan Pinno >   >> print "The password is incorrect."
  > Nathan Pinno >   >> answer = raw_input("What is the password? ")
  > Nathan Pinno >   >> def main_menu():
  > Nathan Pinno >   >> print "1) Add a login info card"
  > Nathan Pinno >   >> print "2) Lookup a login info card"
  > Nathan Pinno >   >> print "3) Remove a login info card"
  > Nathan Pinno >   >> print "4) Print Login info list"
  > Nathan Pinno >   >> print "5) Save login list"
  > Nathan Pinno >   >> print "6) Open Login list"
  > Nathan Pinno >   >> print "9) Exit"
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> def load_login(site,filename):
  > Nathan Pinno >   >> in_file = open(filename,"r")
  > Nathan Pinno >   >> while 1:
  > Nathan Pinno >   >> in_line = in_file.readline()
  > Nathan Pinno >   >> if len(in_file) == 0:
  > Nathan Pinno >   >> break
  > Nathan Pinno >   >> in_line = in_line[:-1]
  > Nathan Pinno >   >> [site,id,passcard] = 
string.split(in_line,",")
  > Nathan Pinno >   >> list[site] = id and passcard
  > Nathan Pinno >   >> in_file.close()
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> def save_login(site,filename):
  > Nathan Pinno >   >> out_file = open(filename,"w")
  > Nathan Pinno >   >> for x in site.keys():
  >
  > site comes form your input :  site = raw_input("Site: ")
  > and is a string and not a dictionary.
  > Perhaps you will use here "sites", but there are no items added to
  > sites??
  >
  >
  > Nathan Pinno >   >> out_file.write(x+","+sites[x]+"\n")
  > Nathan Pinno >   >> out_file.close()
  > Nathan Pinno >   >>
  > Nathan Pinno >   >> menu_choice = "0"
  > Nathan Pinno >   >> list = {}
  > Nathan Pinno >   >> print "Welcome to the second half of the program."
  > Nathan Pinno >   >> print main_menu()
  > Nathan Pinno >   >> while menu_choice != "9":
  > Nathan Pinno >   >> menu_choice = raw_input("Choose an option: ")
  > Nat

[Tutor] Create zip files on the fly.

2005-07-07 Thread Negroup -
Hi, in my application I need to generate a zip file (via the great
zipfile module) and pass it from function A to function B. One way to
do it is create the object from function A and write it on filesystem
via close(). Then, function B will obtain the object reading the file
from disk. After this operation, the file must be deleted from the
filesystem.

Now I'd like to know if it is possible to accomplish this task working
in memory directly without pass for the filesystem. I know that a
class StringIO.StringIO (memory files) exists, but I need something
valid for zipfiles.
As a final solution I have also considered to use tempfile module to
generate files that will be automatically removed.

Sorry for the simple question but I'm just trying to learn.. Do you
have some way to enlighten me?

Thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's wrong with this code?

2005-07-07 Thread Danny Yoo

Hi Nathan,

Other folks are pointing out why you're getting certain error messages.
You may also want to consider breaking up the long second-half of the
program into into its own set of functions.  I believe that the second
half could look something like this:

##
while menu_choice != 9:
menu_choice = input("Choose an option:")
if menu_choice == 1:
add_login_command()
elif menu_choice == 2:
lookup_login_command()
elif menu_choice == 3:
remove_login_command()
elif menu_choice == 4:
display_login_command()
elif menu_choice == 5:
save_login_command()
elif menu_choice == 6:
load_login_command()
##

The idea behind the break-up here is to make it a little easier to see, at
a high level, how the program works.  As a side benefit, it should help to
avoid wacky things like certain menu choices munging up the variables used
by other menu choices.

Finally, by physcially shortening that loop, it's easier to see that we'll
always ask for a menu_choice whenever we hit the start of the loop.  In
contrast, the original code is so long that we may have forgotten, which
is why each menu command appears to repeat the "Choose an option:"
question.

(When you learn a little more about Python, we can talk about the concept
of "dispatch tables" to further dissolve that code, but let's talk about
that later.)

I guess I'm trying to say: style can mattes because it can help you fix
your program's bugs faster.  *grin*


Best of wishes to you!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread ZIYAD A. M. AL-BATLY
On Thu, 2005-07-07 at 00:54 -0600, Nathan Pinno wrote:
> Hi all,
>  
> Here's one of the messages that pops up:
>  
> Traceback (most recent call last):
>   File "D:\password.py", line 77, in ?
> filename == raw_input("Filename to load: ")
> NameError: name 'filename' is not defined
>  
> Why is it popping up whenever I try to load a file?
>  
> Here's the latest code:
>  
> # This is the code for a password protected program to store passwords.
> password = "hello"
> print "The Password Program"
> print "Copyright 2005 Nathan Pinno."
> print
> answer = raw_input("What is the password? ")
> while password != answer:
> print "The password is incorrect."
> answer = raw_input("What is the password? ")
> def main_menu():
> print "1) Add a login info card"
> print "2) Lookup a login info card"
> print "3) Remove a login info card"
> print "4) Print Login info list"
> print "5) Save login list"
> print "6) Open Login list"
> print "9) Exit"
>  
> def load_login(site,filename):
> in_file = open(filename,"r")
> while 1:
> in_line = in_file.readline()
> if len(in_file) == 0:
> break
> in_line = in_line[:-1]
> [site,id,passcard] = string.split(in_line,",")
> list[site] = id and passcard
> in_file.close()
> 
> def save_login(site,filename):
> out_file = open(filename,"w")
> for x in site.keys():
> out_file.write(x+","+sites[x]+"\n")
> out_file.close()
> 
> menu_choice = "0"
> list = {}
> print "Welcome to the second half of the program."
> print main_menu()
Above, you mean:
 main_menu()
instead of "print main_menu()" which is wrong (not technically wrong,
but you what you really want to do).

> while menu_choice != "9":
> menu_choice = raw_input("Choose an option: ")
> if menu_choice == "1":
> print "Add a login info card"
> site = raw_input("Site: ")
> id = raw_input("User ID: ")
> passcard = raw_input("Password: ")
> list[site] = id and passcard
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "2":
> print "Lookup a login info card"
> site = raw_input("Site: ")
> if site.has_key(site):
> print "The ID is: ",id(site)
> print "The password is: ",passcard(site)
> else:
> print site," was not found."
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "3":
> print "Remove a login info card"
> site = raw_input("Site: ")
> if sites.has_key(site):
> del numbers[site]
> else:
> print site," was not found."
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "4":
> print "Login Info"
> for x in site.keys():
> print "Site: ",x," \tID: ",numbers[x]," \tPassword: ",numbers[x]
> print
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "5":
> filename = raw_input("Filename to save: ")
> save_login(list,filename)
> menu_choice = raw_input("Choose an option: ")
> elif menu_choice == "6":
> filename == raw_input("Filename to load: ")
   ^^
  filename = raw_input("Filename to load: ")

You used '==' instead of '='.

> load_login(list,filename)
> menu_choice = raw_input("Choose an option: ")
> print "Have a nice day!"
>  
> Anything else that needs addressing?
>  
> Thanks,
> Nathan Pinno
> http://www.npinnowebsite.ca/

Ziyad.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's wrong with this code?

2005-07-07 Thread Liam Clarke
And please, please, post the whole error message, as it specifies where the error occurred.

Oh, yes, and I think I answered this - 

"
I meant to ask why the main part after the password is not working right.
No one has answered that yet. When I run the code and try to load a file
that has been saved, a TypeError appears.
"

Try id[site] and passcard[site] not id(site) and passcard(site). It'll make a world of difference, honest.On 7/7/05, Danny Yoo <
[EMAIL PROTECTED]> wrote:Hi Nathan,Other folks are pointing out why you're getting certain error messages.
You may also want to consider breaking up the long second-half of theprogram into into its own set of functions.  I believe that the secondhalf could look something like this:##while menu_choice != 9:
menu_choice = input("Choose an option:")if menu_choice == 1:add_login_command()elif menu_choice == 2:lookup_login_command()elif menu_choice == 3:remove_login_command()
elif menu_choice == 4:display_login_command()elif menu_choice == 5:save_login_command()elif menu_choice == 6:load_login_command()##The idea behind the break-up here is to make it a little easier to see, at
a high level, how the program works.  As a side benefit, it should help toavoid wacky things like certain menu choices munging up the variables usedby other menu choices.Finally, by physcially shortening that loop, it's easier to see that we'll
always ask for a menu_choice whenever we hit the start of the loop.  Incontrast, the original code is so long that we may have forgotten, whichis why each menu command appears to repeat the "Choose an option:"
question.(When you learn a little more about Python, we can talk about the conceptof "dispatch tables" to further dissolve that code, but let's talk aboutthat later.)I guess I'm trying to say: style can mattes because it can help you fix
your program's bugs faster.  *grin*Best of wishes to you!___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor-- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding attributes to imported class

2005-07-07 Thread Michael Lange
On Thu, 07 Jul 2005 11:03:00 +1200 (NZST)
[EMAIL PROTECTED] wrote:

> Quoting Michael Lange <[EMAIL PROTECTED]>:
> 
> > I'm trying to write a python wrapper for the tkDnD Tk extension to add
> > drag and drop support
> 
> Hi Michael,
> 
> Just a side issue --- tkDnD seems to be broken in python 2.4: see bug 1164742,
> http://sourceforge.net/tracker/index.php?func=detail&aid=1164742&group_id=5470&atid=105470
> 
> -- 

Hi John,

thanks for the pointer , but of course I did not mean the python module but the 
Tk extension library
( http://sourceforge.net/projects/tkdnd ), which adds "real" dnd support to Tk.

As far as I have tested (not very much however) it seems to work now, but I'm 
still not sure if the on-the-fly adding of attributes
to classes of an imported module is proper programming style or "do-it-yourself 
lobotomy" .
It is the first time for me to try wrapping a Tk extension for python, so I am 
not sure of the way to go.

Anyway, it's a cool new feature for Tkinter, so I thought I should share it 
with the community;
anyone who is interested can find it at 
http://www.8ung.at/klappnase/TkinterDnD/TkinterDnD.html .

Any feedback is much appreciated.

Best regards

Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Mohammad Moghimi
Hi there
I wrote a recursive function a got this error how can I increase 
maximum recursion depth. And what is its default value?
-- Mohammad
do you Python?!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

2005-07-07 Thread Ewald Ertl
Hello!

I just looked a little over the code, what it is perhaps ment to do now. 

As allready mentioned by Ziyad in your former post:

just call "main_menu()" 

don't print main_menu()

This calls the funciton main_men(), which prints it menu and 
after this the print-Command in this line print's the return-Value 
of the function : 
e.g. in the python-shell:

>>> def main_menu():
... print "here in main_menu"
... 
>>> print main_menu()
here in main_menu
None


The next thing is to move main_menu() into the while-Loop, 
so you see the menu every time the loop runs, otherwise it would 
after some inputs disappear from the screen. 
In every if/elif-branch you can delete the last raw_input() requesting 
for an option, because the while-Loop has it's own request at the start of
the loop. 

Here's a snap of the first 2 Options in the menu, I've changed: 
-
while menu_choice != "9":
main_menu()
menu_choice = raw_input("Choose an option: ")
if menu_choice == "1":
print "Add a login info card"
site = raw_input("Site: ")
id = raw_input("User ID: ")
passcard = raw_input("Password: ")
sitelist[site] = [id,passcard]
 Here I asign the data to the dictionary sitelist with the key "site" 
 an list with the value of id as the first entry and the value of passcard
 as the second entry
 I've renamed list to sitelist, because list itself is a builtin creating 
 a list . 
 e.g.:
 >>> a="hallo"
 >>> list(a)
 ['h', 'a', 'l', 'l', 'o']


elif menu_choice == "2":
print "Lookup a login info card"
site = raw_input("Site: ")
 as the "list"  is a dictionary we can lookup if the site is 
 available in the sitelist. 
if sitelist.has_key(site):  
print "The ID is: ",sitelist[site][0]
 the 0 Element of the list at sitelist[site] is the id and the first 
 one is the password. 

print "The password is: ",sitelist[site][1]
else:
print site," was not found."
-

I've used a list for storing the elements of id and passcard, but you could 
also use an other 
dictionary with the keys id and passcard if you like. 

The next problem is in the part of menu_choice =="3" and "4":
There is no dictionary named "numbers". Here you can also use the 
sitelist[site], ... 

I hope that help's you to change the rest of your code. 


Ewald 




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Wolfram Kraus
Mohammad Moghimi wrote:
> Hi there
> I wrote a recursive function a got this error how can I increase 
> maximum recursion depth. And what is its default value?
> -- Mohammad
> do you Python?!!

Start your python interpreter and:

 >>> import sys
 >>> sys.getrecursionlimit()
1000
 >>> help(sys.setrecursionlimit)

HTH,
Wolfram

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Ewald Ertl
Hi Mohammad!

I've never use a recursive function in python, but the error has the 
same meaning as e.g in java. 

Your function get's called and called without the end-criteria for the 
recursive call is ever met. 
I would suggest to check if the end-criteria of the recursive call is 
correct. 

I don't know if there is something like a "default" value for the maximum of 
a recursive call depth.

I just looked into the sys-Module and found the following on my Solaris 9 box:

>>> help(sys.setrecursionlimit)
Help on built-in function setrecursionlimit:

setrecursionlimit(...)
setrecursionlimit(n)

Set the maximum depth of the Python interpreter stack to n.  This
limit prevents infinite recursion from causing an overflow of the C
stack and crashing Python.  The highest possible limit is platform-
dependent.

>>> sys.getrecursionlimit()
1000
>>> 


HTH Ewald 

on Thu, 7 Jul 2005 14:54:16 +0430  Mohammad Moghimi <[EMAIL PROTECTED]> wrote :
-

Mohammad Moghimi > Hi there
Mohammad Moghimi > I wrote a recursive function a got this error how can I 
increase 
Mohammad Moghimi > maximum recursion depth. And what is its default value?
Mohammad Moghimi > -- Mohammad
Mohammad Moghimi > do you Python?!!
Mohammad Moghimi > ___
Mohammad Moghimi > Tutor maillist  -  Tutor@python.org
Mohammad Moghimi > http://mail.python.org/mailman/listinfo/tutor
Mohammad Moghimi > 


--- end --


-- 
Ing. Ewald Ertl HartterGruppe   Phone : 
+43-3352-33085-558
trinomic Projektmanagement & Informationstechnik GmbH   Fax   : 
+43-3352-33085-600
Wiener Straße 41mailto:[EMAIL PROTECTED]
A-7400 Oberwart http://www.trinomic.com mailto:[EMAIL PROTECTED]

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's wrong with this code?

2005-07-07 Thread Andre Engels
On 7/7/05, Nathan Pinno <[EMAIL PROTECTED]> wrote:
>   Hi all,
> 
>   I meant to ask why the main part after the password is not working right.
> No one has answered that yet. When I run the code and try to load a file
> that has been saved, a TypeError appears. How do I fix the code so no more
> errors will show up. Here is the newest code so far:

I don't get a TypeError, but a NameError. Reason is the line:
filename == raw_input("Filename to load: ")
which should be:
filename = raw_input("Filename to load: ")

After correcting this, I get the TypeError. And the error clearly says
what is wrong: in_file is a file object, and those do not have the
value "len". I think what you mean is done by replacing:
if len(in_file) == 0:
by:
if len(in_line) == 0:

In general, in such cases, check the error message. In this case it
said "len() of an unsized object". There is only one len() in the line
given, so that must be it, and thus in_file must be that 'unsized
object'.

Andre Engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] a problem wiht struct module

2005-07-07 Thread Mohammad Moghimi
Hi there
can you describe why last two lines print different numbers

---
import struct
struct.calcsize("lH")
struct.calcsize("Hl")
---

-- Mohammad
do you Python?!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create zip files on the fly.

2005-07-07 Thread Kent Johnson
Negroup - wrote:
> Hi, in my application I need to generate a zip file (via the great
> zipfile module) and pass it from function A to function B. One way to
> do it is create the object from function A and write it on filesystem
> via close(). Then, function B will obtain the object reading the file
> from disk. After this operation, the file must be deleted from the
> filesystem.
> 
> Now I'd like to know if it is possible to accomplish this task working
> in memory directly without pass for the filesystem. I know that a
> class StringIO.StringIO (memory files) exists, but I need something
> valid for zipfiles.

The ZipFile constructor accepts a "file-like object"; that is, any object that 
supports the same interface as a file. An instance of cStringIO.StringIO should 
work fine here. Something like
out = cStringIO.StringIO()
zip = zipfile.ZipFile(out, 'w', zipfile.ZIP_DEFLATED)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a problem wiht struct module

2005-07-07 Thread Kent Johnson
Mohammad Moghimi wrote:
> Hi there
> can you describe why last two lines print different numbers
> 
> ---
> import struct
> struct.calcsize("lH")
> struct.calcsize("Hl")
> ---

Because by default struct uses "native" alignment. Your processor presumably 
aligns longs on long boundaries so two pad bytes are inserted in the Hl case. 
You can force no alignment by using "standard" alignment. For example on 
Windows:

 >>> from struct import calcsize
 >>> calcsize('Hl')
8
 >>> calcsize('lH')
6
 >>> calcsize('=Hl')
6
 >>> calcsize('=lH')
6

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python and database

2005-07-07 Thread Cedric BRINER
Hi,

I've read this:
http://www.python.org/workshops/1997-10/proceedings/shprentz.html
and some points look good:
 - no hand coded SQL queries
 - provide a cache
 - objects insert and updates
 - ...
but quite old too.

So the question is : what are the best practice to use python with a pgsql.

Ced.
-- 

Cedric BRINER
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] generating documentation for a module

2005-07-07 Thread Mike Hansen
Is there a utility that generates documentation for your python 
modules/scripts/programs based on their doc strings that looks similar to the 
library documentation on the python web site?

What do you use to generate documentation for your python 
modules/scripts/programs?

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating documentation for a module

2005-07-07 Thread Wolfram Kraus
Mike Hansen wrote:
> Is there a utility that generates documentation for your python 
> modules/scripts/programs based on their doc strings that looks similar to the 
> library documentation on the python web site?
> 
> What do you use to generate documentation for your python 
> modules/scripts/programs?
> 
> Mike

Never used it, but this should do what you want:

http://docs.python.org/lib/module-pydoc.html

Batteries included ;-)
HTH,
Wolfram

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dict operation question.

2005-07-07 Thread David Driver
I have a function

def updateit(self,**mydict):

which is intended to update a dictionary self.somedict. 

If there are keys in mydict that are not in self.somedict I want them
returned in a new dict.

here is what i am doing now:

errdict = dict([(a,b) for a,b in mydict.items() if not a in self.somedict])

I then test (if errdict) and raise an exception that contains errdict
as an attribute. Else i update self.somedict.

Is there something other than a list comprehension in a dict function
that can do this?

Thanks!
-- 

***
See there, that wasn't so bad.
***
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Can't figure out AttributeError message

2005-07-07 Thread Jim Roush
I'm getting the following error message:

  AttributeError: 'tuple' object has no attribute 'seek'

below is the code that produced the error.  The line in question is 
marked with arrow in the left margin.  Any help would be appreciated. 

def build_sp500_rand():
sp500_csv = open('c:/indices/sp500.csv', 'r')
sp500_csv_recs = sp500_csv.readlines()
sp500_csv.close()
   
sp_rand = ('c:/indices/sp500.rnd', 'w+b')
record_size  = struct.calcsize('Lf')

record_number = 0
for rec in sp500_csv_recs:
rec.strip('\n')
sp500_csv_fields = rec.split(',')
   
   
sp500_csv_date = sp500_csv_fields[0]
sp500_csv_open = sp500_csv_fields[1]
sp500_csv_high = sp500_csv_fields[2]
sp500_csv_low = sp500_csv_fields[3]
sp500_csv_close = sp500_csv_fields[4]
sp500_csv_volume = sp500_csv_fields[5]
   
print 'build:', sp500_csv_date
   
buffer = struct.pack('Lf', long(sp500_csv_date), 
float(sp500_csv_close))
>   sp_rand.seek(record_number * record_size)
sp_rand.write(buffer)
   
record_number = record_number + 1
   
sp_rand.close()




-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.10/43 - Release Date: 7/6/2005

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't figure out AttributeError message

2005-07-07 Thread Reed L. O'Brien
Jim Roush wrote:

>I'm getting the following error message:
>
>  AttributeError: 'tuple' object has no attribute 'seek'
>
>below is the code that produced the error.  The line in question is 
>marked with arrow in the left margin.  Any help would be appreciated. 
>
>def build_sp500_rand():
>sp500_csv = open('c:/indices/sp500.csv', 'r')
>sp500_csv_recs = sp500_csv.readlines()
>sp500_csv.close()
>   
>sp_rand = ('c:/indices/sp500.rnd', 'w+b')
>record_size  = struct.calcsize('Lf')
>
>record_number = 0
>for rec in sp500_csv_recs:
>rec.strip('\n')
>sp500_csv_fields = rec.split(',')
>   
>   
>sp500_csv_date = sp500_csv_fields[0]
>sp500_csv_open = sp500_csv_fields[1]
>sp500_csv_high = sp500_csv_fields[2]
>sp500_csv_low = sp500_csv_fields[3]
>sp500_csv_close = sp500_csv_fields[4]
>sp500_csv_volume = sp500_csv_fields[5]
>   
>print 'build:', sp500_csv_date
>   
>buffer = struct.pack('Lf', long(sp500_csv_date), 
>float(sp500_csv_close))
>>   sp_rand.seek(record_number * record_size)
>sp_rand.write(buffer)
>   
>record_number = record_number + 1
>   
>sp_rand.close()
>
>
>
>
>  
>
>>> sp_rand = ('c:/indices/sp500.rnd', 'w+b')
>>> type(sp_rand)


you aren't opening sp_500.rnd you are assigning tuple values.

>>> sp_rand = open('c:/indices/sp500.rnd', 'w+b')
>>> type(sp_rand)


but you still need to read in values (sp_rand.readlines()) assuming
that's what you want.

~r
-- 
4.6692916090
'cmVlZG9icmllbkBnbWFpbC5jb20=\n'.decode('base64')
http://www.spreadfirefox.com/?q=affiliates&id=16474&t=1
keyID: 0x0FA09FCE

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't figure out AttributeError message

2005-07-07 Thread Brian van den Broek
Jim Roush said unto the world upon 07/07/2005 12:42:
> I'm getting the following error message:
> 
>   AttributeError: 'tuple' object has no attribute 'seek'
> 
> below is the code that produced the error.  The line in question is 
> marked with arrow in the left margin.  Any help would be appreciated. 
> 
> def build_sp500_rand():
> sp500_csv = open('c:/indices/sp500.csv', 'r')
> sp500_csv_recs = sp500_csv.readlines()
> sp500_csv.close()
>
> sp_rand = ('c:/indices/sp500.rnd', 'w+b')

Here is the problem. sp_rand is a tuple of 2 strings, not a file 
object. Try
   sp_rand = open('c:/indices/sp500.rnd', 'w+b')
or
   sp_rand = file('c:/indices/sp500.rnd', 'w+b')



> record_size  = struct.calcsize('Lf')
> 
> record_number = 0
> for rec in sp500_csv_recs:



> buffer = struct.pack('Lf', long(sp500_csv_date), 
> float(sp500_csv_close))
> >   sp_rand.seek(record_number * record_size)
> sp_rand.write(buffer)
>
> record_number = record_number + 1
>
> sp_rand.close()

HTH,

Brian vdB


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Nathan Pinno




  Hi all,
   
  Why does invalid syntax popup?
   
  Here is the latest code:
  # This is the code for a password protected program to store 
  passwords.password = "hello"print "The Password Program"print 
  "Copyright 2005 Nathan Pinno."printanswer = raw_input("What is the 
  password? ")while password != answer:    print "The 
  password is incorrect."    answer = raw_input("What is the 
  password? ")def main_menu_command():    print "1) Add a 
  login info card"    print "2) Lookup a login info 
  card"    print "3) Remove a login info 
  card"    print "4) Print Login info 
  list"    print "5) Save login list"    
  print "6) Open Login list"    print "9) Exit"
   
  def load_login_command(site,filename):    in_file = 
  open(filename,"r")    while 
  1:    in_line = 
  in_file.readline()    if 
  len(in_file) == 
  0:    
  break    in_line = 
  in_line[:-1]    [site,id,passcard] 
  = string.split(in_line,",")    
  list[site] = id and passcard    
  in_file.close()    def 
  save_login_command(site,filename):    out_file = 
  open(filename,"w")    for x in 
  site.keys():    
  out_file.write(x+","+sites[x]+"\n")    
  out_file.close()
   
  def add_login_command(site,filename):    print "Add a 
  login info card"    site = raw_input("Site: 
  ")    id = raw_input("User ID: ")    
  passcard = raw_input("Password: ")    sitelist[site] = 
  [id,passcard]
   
  def lookup_login_command(site,filename):    print 
  "Lookup a login info card"    site 
  = raw_input("Site: ")    if 
  sitelist.has_key(site):    
  print "The ID is: 
  ",sitelist[site][0]    
  print "The password is: 
  ",sitelist[site][1]    
  else:    
  print site," was not found."
   
  def remove_login_command(site,filename):    print 
  "Remove a login info card"    site 
  = raw_input("Site: ")    if 
  sites.has_key(site):    
  del sitelist[site]    
  else:    
  print site," was not found."
   
  def display_login_command(site,filename):    print 
  "Login Info"    for x in 
  site.keys():    
  print "Site: ",sitelist," \tID: ",sitelist[site]," \tPassword: 
  ",sitelist[site]    print
   
  menu_choice = "0"list = {}print "Welcome to the second half 
  of the program."main_menu()while menu_choice != 
  "9":    menu_choice = raw_input("Choose an option: 
  ")    if menu_choice == 
  "1":    
  add_login_command()    elif menu_choice == 
  "2":    
  lookup_login_command()    elif menu_choice == 
  "3":    
  remove_login_command()    elif menu_choice == 
  "4":    
  display_login_command()    elif menu_choice == 
  "5":    filename = 
  raw_input("Filename to save: ")    
  save_login_command()    elif menu_choice == 
  "6":    filename = 
  raw_input("Filename to load: ")    
  load_login_command()print "Have a nice day!"
   
  Thanks,
  Nathan Pinnohttp://www.npinnowebsite.ca/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Danny Yoo


On Thu, 7 Jul 2005, Nathan Pinno wrote:

>   Why does invalid syntax popup?

[text cut]

Hi Nathan,

What does the SyntaxError look like?  Copy-and-paste that error message
and bring it to the Tutor list.  One thing we want to help you do is
recognize what the SyntaxError is really trying to say, so that the next
time you see a SyntaxError, you have ways to fix it.

Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Nathan Pinno
  Here is the error:

File "D:\password.py", line 45
  site = raw_input("Site: ")
  ^
  SyntaxError: invalid syntax

  HTH,
  Nathan Pinno
  - Original Message - 
  From: "Danny Yoo" <[EMAIL PROTECTED]>
  To: "Nathan Pinno" <[EMAIL PROTECTED]>
  Cc: 
  Sent: Thursday, July 07, 2005 11:53 AM
  Subject: Re: [Tutor] Why does invalid syntax pop up?


  >
  >
  > On Thu, 7 Jul 2005, Nathan Pinno wrote:
  >
  >>   Why does invalid syntax popup?
  >
  > [text cut]
  >
  > Hi Nathan,
  >
  > What does the SyntaxError look like?  Copy-and-paste that error message
  > and bring it to the Tutor list.  One thing we want to help you do is
  > recognize what the SyntaxError is really trying to say, so that the next
  > time you see a SyntaxError, you have ways to fix it.
  >
  > Best of wishes!
  >
  >
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Nathan Pinno
  Here is another error message:

  Traceback (most recent call last):
File "D:\password.py", line 69, in ?
  main_menu()
  NameError: name 'main_menu' is not defined

  Thanks.
  - Original Message - 
  From: "Nathan Pinno" <[EMAIL PROTECTED]>
  To: "Danny Yoo" <[EMAIL PROTECTED]>
  Cc: 
  Sent: Thursday, July 07, 2005 1:04 PM
  Subject: Re: [Tutor] Why does invalid syntax pop up?


  >  Here is the error:
  >
  >File "D:\password.py", line 45
  >  site = raw_input("Site: ")
  >  ^
  >  SyntaxError: invalid syntax
  >
  >  HTH,
  >  Nathan Pinno
  >  - Original Message - 
  >  From: "Danny Yoo" <[EMAIL PROTECTED]>
  >  To: "Nathan Pinno" <[EMAIL PROTECTED]>
  >  Cc: 
  >  Sent: Thursday, July 07, 2005 11:53 AM
  >  Subject: Re: [Tutor] Why does invalid syntax pop up?
  >
  >
  >  >
  >  >
  >  > On Thu, 7 Jul 2005, Nathan Pinno wrote:
  >  >
  >  >>   Why does invalid syntax popup?
  >  >
  >  > [text cut]
  >  >
  >  > Hi Nathan,
  >  >
  >  > What does the SyntaxError look like?  Copy-and-paste that error 
message
  >  > and bring it to the Tutor list.  One thing we want to help you do is
  >  > recognize what the SyntaxError is really trying to say, so that the 
next
  >  > time you see a SyntaxError, you have ways to fix it.
  >  >
  >  > Best of wishes!
  >  >
  >  >
  > ___
  > Tutor maillist  -  Tutor@python.org
  > http://mail.python.org/mailman/listinfo/tutor
  >
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Nathan Pinno
  Here is the latest code, in case you need it:

  # This is the code for a password protected program to store passwords.
  password = "hello"
  print "The Password Program"
  print "Copyright 2005 Nathan Pinno."
  print
  answer = raw_input("What is the password? ")
  while password != answer:
  print "The password is incorrect."
  answer = raw_input("What is the password? ")
  def main_menu_command():
  print "1) Add a login info card"
  print "2) Lookup a login info card"
  print "3) Remove a login info card"
  print "4) Print Login info list"
  print "5) Save login list"
  print "6) Open Login list"
  print "9) Exit"

  def load_login_command(site,filename):
  in_file = open(filename,"r")
  while 1:
  in_line = in_file.readline()
  if len(in_file) == 0:
  break
  in_line = in_line[:-1]
  [site,id,passcard] = string.split(in_line,",")
  list[site] = id and passcard
  in_file.close()

  def save_login_command(site,filename):
  out_file = open(filename,"w")
  for x in site.keys():
  out_file.write(x+","+sites[x]+"\n")
  out_file.close()

  def add_login_command(site,filename):
  print "Add a login info card"
  site = raw_input("Site: ")
  id = raw_input("User ID: ")
  passcard = raw_input("Password: ")
  sitelist[site] = [id,passcard]

  def lookup_login_command(site,filename):
  print "Lookup a login info card"
  site = raw_input("Site: ")
  if sitelist.has_key(site):
  print "The ID is: ",sitelist[site][0]
  print "The password is: ",sitelist[site][1]
  else:
  print site," was not found."

  def remove_login_command(site,filename):
  print "Remove a login info card"
  site = raw_input("Site: ")
  if sites.has_key(site):
  del sitelist[site]
  else:
  print site," was not found."

  def display_login_command(site,filename):
  print "Login Info"
  for x in site.keys():
  print "Site: ",sitelist," \tID: ",sitelist[site]," \tPassword: 
",sitelist[site]
  print

  menu_choice = "0"
  list = {}
  print "Welcome to the second half of the program."
  main_menu()
  while menu_choice != "9":
  menu_choice = raw_input("Choose an option: ")
  if menu_choice == "1":
  add_login_command()
  elif menu_choice == "2":
  lookup_login_command()
  elif menu_choice == "3":
  remove_login_command()
  elif menu_choice == "4":
  display_login_command()
  elif menu_choice == "5":
  filename = raw_input("Filename to save: ")
  save_login_command()
  elif menu_choice == "6":
  filename = raw_input("Filename to load: ")
  load_login_command()
  print "Have a nice day!"

  HTH,
  Nathan Pinno.
  - Original Message - 
  From: "Nathan Pinno" <[EMAIL PROTECTED]>
  To: "Danny Yoo" <[EMAIL PROTECTED]>
  Cc: 
  Sent: Thursday, July 07, 2005 1:09 PM
  Subject: Re: [Tutor] Why does invalid syntax pop up?


  >  Here is another error message:
  >
  >  Traceback (most recent call last):
  >File "D:\password.py", line 69, in ?
  >  main_menu()
  >  NameError: name 'main_menu' is not defined
  >
  >  Thanks.
  >  - Original Message - 
  >  From: "Nathan Pinno" <[EMAIL PROTECTED]>
  >  To: "Danny Yoo" <[EMAIL PROTECTED]>
  >  Cc: 
  >  Sent: Thursday, July 07, 2005 1:04 PM
  >  Subject: Re: [Tutor] Why does invalid syntax pop up?
  >
  >
  >  >  Here is the error:
  >  >
  >  >File "D:\password.py", line 45
  >  >  site = raw_input("Site: ")
  >  >  ^
  >  >  SyntaxError: invalid syntax
  >  >
  >  >  HTH,
  >  >  Nathan Pinno
  >  >  - Original Message - 
  >  >  From: "Danny Yoo" <[EMAIL PROTECTED]>
  >  >  To: "Nathan Pinno" <[EMAIL PROTECTED]>
  >  >  Cc: 
  >  >  Sent: Thursday, July 07, 2005 11:53 AM
  >  >  Subject: Re: [Tutor] Why does invalid syntax pop up?
  >  >
  >  >
  >  >  >
  >  >  >
  >  >  > On Thu, 7 Jul 2005, Nathan Pinno wrote:
  >  >  >
  >  >  >>   Why does invalid syntax popup?
  >  >  >
  >  >  > [text cut]
  >  >  >
  >  >  > Hi Nathan,
  >  >  >
  >  >  > What does the SyntaxError look like?  Copy-and-paste that error
  > message
  >  >  > and bring it to the Tutor list.  One thing we want to help you do 
is
  >  >  > recognize what the SyntaxError is really trying to say, so that 
the
  > next
  >  >  > time you see a SyntaxError, you have ways to fix it.
  >  >  >
  >  >  > Best of wishes!
  >  >  >
  >  >  >
  >  > ___
  >  > Tutor maillist  -  Tutor@python.org
  >  > http://mail.python.org/mailman/listinfo/tutor
  >  >
  > ___
  > Tutor maillist  -  Tutor@python.org
  > http://mail.python.org/mailman/listinfo/tutor
  >
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating documentation for a module

2005-07-07 Thread Mike Hansen
> Subject:
> Re: [Tutor] generating documentation for a module
> From:
> Wolfram Kraus <[EMAIL PROTECTED]>
> Date:
> Thu, 07 Jul 2005 16:17:11 +0200
> To:
> tutor@python.org
> 
> To:
> tutor@python.org
> 
> 
> Mike Hansen wrote:
> 
>> Is there a utility that generates documentation for your python 
>> modules/scripts/programs based on their doc strings that looks similar 
>> to the library documentation on the python web site?
>>
>> What do you use to generate documentation for your python 
>> modules/scripts/programs?
>>
>> Mike
> 
> 
> Never used it, but this should do what you want:
> 
> http://docs.python.org/lib/module-pydoc.html
> 
> Batteries included ;-)
> HTH,
> Wolfram
> 

Duh... I should have noticed that. The html output is pretty good.

Thanks,

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Getting started in jython

2005-07-07 Thread Andrew D. Fant
I'm a semi-competent python user who has discovered that all the
toolkits I really have use of are bound up in java classes.  I can cope
with this, because there is Python, but I was wondering if anyone who
has spent more time around it than I have could spare any advice that
will make the adjustment a little less painful.

Thanks,
Andy
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Alan G
> I wrote a recursive function a got this error how can 
> I increase maximum recursion depth. 

You have to go into the Python source code and change a 
value then rebuild Python.

> And what is its default value?

1000 last time I looked.

But beware that increasing it too far can result in 
significant memory usage, which is why there is a limit 
in the first place. Recursion is a powerful tool but 
can quickly run away with your PC resources. Although 
with modern machines you can probably increase the 
limit to 1 without serious problems - but I've 
never tried!

Maybe someday someone will convert Python to implement 
tail-end recursion...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Alan G
> Start your python interpreter and:
>
>  >>> import sys
>  >>> sys.getrecursionlimit()
> 1000
>  >>> help(sys.setrecursionlimit)

Ooh, that's nice. When did the ability to tweak it programmatically
appear?
Or has it always been there and I've just never noticed?
Looks like a good candidate for use inside an exception handler...

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Danny Yoo


On Thu, 7 Jul 2005, Nathan Pinno wrote:

>   Here is another error message:
>
>   Traceback (most recent call last):
> File "D:\password.py", line 69, in ?
>   main_menu()
>   NameError: name 'main_menu' is not defined


Hi Nathan,

The error message is correct: there is no "main_menu()" function.  There
is, however, a "main_menu_command()" function that's defined right at the
top.  Did you mean that instead?

And just out of curiosity, when did you get this NameError message?
Before the SyntaxError, or after?  The reason I ask is because if you're
still hitting SyntaxError, there should no be possible way for the program
to even get to NameError --- a SyntaxError is a show-stopper.

Is the SyntaxError still showing up?  As far as I could tell, your code
looked fine from a syntactic point of view.



Best of wishes to you!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Alan G

>  Why does invalid syntax popup?

If you show us the error message it will probably tell us.
Thats what the error messages are for. They may look cryptic when 
you start out but they generally give you a clue as to roughly 
the right bit of the program and the reason.

Can you repost with the error to save us reading and analysing 
all of your code.

Thanks,

Now a quick scan through shows up some strangely indented print 
statements, is it those that are being complained about:


  def lookup_login_command(site,filename):
  print "Lookup a login info card"

there is an indentation mismatch between these lines.

  site = raw_input("Site: ")
  if sitelist.has_key(site):
  print "The ID is: ",sitelist[site][0]
  print "The password is: ",sitelist[site][1]
  else:
  print site," was not found."

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting started in jython

2005-07-07 Thread Alan G

> I'm a semi-competent python user who has discovered that all the
> toolkits I really have use of are bound up in java classes.  I can
cope
> with this, because there is Python, but I was wondering if anyone
who
> has spent more time around it than I have could spare any advice
that
> will make the adjustment a little less painful.

Just go through the Jython tutorial.

I found the O'Reilly book useful too but it does cover a lot of basic
Python stuff if you already know standard Python. But since I have a
Safari
subscription it didn't cost me anything!

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting started in jython

2005-07-07 Thread D. Hartley
I don't know if this will be useful to everyone, but I found the
O'Reilly book wasn't so helpful to those starting with Python and
moving to Jython (i.e., rather than starting with Java and not knowing
Python).  I have yet to find Jython materials that are very good for
beginners - most everything I come across is directed at Java
programmers who need to learn enough Python to make Jython work.

~Denise

On 7/7/05, Alan G <[EMAIL PROTECTED]> wrote:
> 
> > I'm a semi-competent python user who has discovered that all the
> > toolkits I really have use of are bound up in java classes.  I can
> cope
> > with this, because there is Python, but I was wondering if anyone
> who
> > has spent more time around it than I have could spare any advice
> that
> > will make the adjustment a little less painful.
> 
> Just go through the Jython tutorial.
> 
> I found the O'Reilly book useful too but it does cover a lot of basic
> Python stuff if you already know standard Python. But since I have a
> Safari
> subscription it didn't cost me anything!
> 
> Alan G.
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dict operation question.

2005-07-07 Thread Javier Ruere
David Driver wrote:
> I have a function
> 
> def updateit(self,**mydict):
> 
> which is intended to update a dictionary self.somedict. 
> 
> If there are keys in mydict that are not in self.somedict I want them
> returned in a new dict.
> 
> here is what i am doing now:
> 
> errdict = dict([(a,b) for a,b in mydict.items() if not a in self.somedict])
> 
> I then test (if errdict) and raise an exception that contains errdict
> as an attribute. Else i update self.somedict.
> 
> Is there something other than a list comprehension in a dict function
> that can do this?
> 
> Thanks!

Hi,
You could have posted the code but anyway, this is your algorithm as I 
understood it:

class Test:
def __init__(self, dict):
self.somedict = dict

def updateit(self, **mydict):
errdict = dict([(a,b) for a,b in mydict.items() if not a in 
self.somedict])

if (not errdict):
raise Exception(errdict)

self.somedict.update(errdict)


The following implementation uses Sets to find the new keys and is slower 
(twice as slow).

from sets import Set

class Test:
def __init__(self, dict):
self.somedict = dict

def updateit(self, **mydict):
new = Set(mydict.iterkeys())
cur = Set(self.somedict.iterkeys())
diff = new - cur

for k in diff:
self.somedict[k] = mydict[k]

if (not diff):
raise Exception(errdict)

The last one uses dicts and is a bit faster than the original.

class Test:
def __init__(self, dict):
self.somedict = dict

def updateit(self, **mydict):
aux = mydict.copy()
aux.update(self.somedict)

size = len(self.somedict)
self.somedict.update(aux)

if (len(self.somedict) == size):
raise Exception({}) # It would be empty.


Javier

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dict operation question.

2005-07-07 Thread Javier Ruere
I should also add the most simple and obvious implementation which is also the 
fastest:


class Test:
def __init__(self, dict):
self.somedict = dict

def updateit(self, **mydict):
errdict = {}
for k, v in mydict.iteritems():
if k not in self.somedict:
self.somedict[k] = v
errdict[k] = v

if (not errdict):
raise Exception(errdict)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion depth exceeded !

2005-07-07 Thread Wolfram Kraus
Alan G wrote:
>> Start your python interpreter and:
>> 
> import sys sys.getrecursionlimit()
>> 1000
> help(sys.setrecursionlimit)
> 
> 
> Ooh, that's nice. When did the ability to tweak it programmatically 
> appear? Or has it always been there and I've just never noticed?

It isn't in the 1.5.2 documentation but in the 2.0 documentation, so
it has been there for some time ;-)

> Looks like a good candidate for use inside an exception handler...
> 
> Alan G.
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does invalid syntax pop up?

2005-07-07 Thread Terry Carroll
On Thu, 7 Jul 2005, Nathan Pinno wrote:

>   Why does invalid syntax popup?




>   def lookup_login_command(site,filename):
>   print "Lookup a login info card"
>   site = raw_input("Site: ")
>   if sitelist.has_key(site):
>   print "The ID is: ",sitelist[site][0]
>   print "The password is: ",sitelist[site][1]
>   else:
>   print site," was not found."

Bad indentation.  Try this instead:


def lookup_login_command(site,filename):
print "Lookup a login info card"
site = raw_input("Site: ")
if sitelist.has_key(site):
print "The ID is: ",sitelist[site][0]
print "The password is: ",sitelist[site][1]
else:
print site," was not found."

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor