Click to See Complete Forum and Search --> : I have a few Python questions:
Gnu/Vince
12-31-2001, 12:01 AM
1. How do I trap a signal (like the interrupt signal) anywhere in the program so that it will execute an action? In Ruby the code would be this:
trap("INT") do
puts "You pressed Ctrl-C. Exiting"
exit
end
2. How do I execute commands in Python while remaining in the program? If I use os.execv or os.execl, after the command is issued, I am thrown out of the program. Is there not a equivalent for system() in Python? Also, if there is a such thing, can you store the output of that conmmand into a variable instead of displaying it on the screen?
3. Where can I find all the exceptions that a function can rise? Is there something such as function.__exceptions__ ?
4. Is there a way to declare a variable and affect it the value NULL (or nil) ?
5. Is there a difference between using single quotes or double quotes and triple quotes or double quotes?
6. What does pass do?
7. What is the proper way to use global variables in Python (I know it's bad programing practice, but I still find it useful to know)
8. Does something like this exist in Python:
while not EOF:
<stuff>
that's it for now, I expect to have more questions soon
[ 30 December 2001: Message edited by: Gnu/Vince ]
Strike
12-31-2001, 12:36 AM
1. Like I told you, wrap the whole thing in a big try block and catch the exception that the signal raises.
try:
raw_input("Waiting...")
except KeyboardInterrupt:
from sys import exit
print "Received KeyboardInterrupt"
exit(1)
2. os.system() is what you want. You may be able to manipulate sys.stdin to get input from the process, but I wouldn't even begin to try to code something to demonstrate that. Are you positive there's not a module in Python that will do what you need, instead of using another program?
3. You can use the dir() command to get a listing of all the contents of a module (including __builtins__ for built-in functions).
4. foo = None
5. No difference, use what you like. They all delimit strings, each ignoring the other as string delimiters (well, obviously " won't ignore """ as a string delimiter, but you get what I mean)
6. pass does nothing - it is a placeholder
7. You know how to use globals in Python, you had it in your RPS program. I just hardly ever see a need to use them.
8. If you want to read from a file (actually, file descriptor), open it and use the commands for file objects - http://www.python.org/doc/current/lib/bltin-file-objects.html
[ 30 December 2001: Message edited by: Strike ]
Gnu/Vince
12-31-2001, 12:59 AM
1. I know, but you know, it doesn't feel right.
2. I doubt that Python has a built-in /usr/games/banner
7. Yeah, but I didn't have to use global in main. Could it be because I didn't modify the value of the variable?
8. I mean to stop doing the action if the user Ctrl-D's.
Strike
12-31-2001, 01:05 AM
Originally posted by Gnu/Vince:
<STRONG>1. I know, but you know, it doesn't feel right.</STRONG>
Well, it works, and it's what I know. Like I said, there may be other ways but I don't know them (and since Python is generally not so TIMTOWTDI as Perl, this might be it)
<STRONG>2. I doubt that Python has a built-in /usr/games/banner</STRONG>
Nope. But a module could be coded for it. Most non-trivial things have a good Python module coded, let's put it that way.
<STRONG>7. Yeah, but I didn't have to use global in main. Could it be because I didn't modify the value of the variable?</STRONG>
I think the global keyword basically states that it will be necessary to look in the global namespace (that is, outside of any functions) for the value of that particular variable. For really technical details - http://www.python.org/doc/current/ref/global.html
<STRONG>8. I mean to stop doing the action if the user Ctrl-D's.</STRONG>
And that is different from normal files how? Everything in Unix is a file descriptor (practically), including STDIN. Just use sys.stdin as your file.
jemfinch
12-31-2001, 03:49 AM
Originally posted by Gnu/Vince:
1. How do I trap a signal (like the interrupt signal) anywhere in the program so that it will execute an action? In Ruby the code would be this:
trap("INT") do
puts "You pressed Ctrl-C. Exiting"
exit
end
Look at the "signal" module.
3. Where can I find all the exceptions that a function can rise? Is there something such as function.__exceptions__ ?
Do you mean the built-in functions? You'll find all that information in the documentation. If you mean user-defined functions, you'll have to read the source.
5. Is there a difference between using single quotes or double quotes and triple quotes or double quotes?
Single quotes and double quotes are the same, except you can nest single quotes in double quotes without escaping and vice versa.
Triple quotes are used for string literals that span multiple lines.
6. What does pass do?
It's a syntactically correct way to say "do nothing"
7. What is the proper way to use global variables in Python (I know it's bad programing practice, but I still find it useful to know)
Just use them.
8. Does something like this exist in Python:
while not EOF:
<stuff>
for line in file.readlines():
stuff
or
while 1:
s = file.read(8192)
if s = "": break
stuff
Jeremy
jemfinch
12-31-2001, 03:51 AM
Originally posted by Gnu/Vince:
8. I mean to stop doing the action if the user Ctrl-D's.
Yeah, the way I showed you before. If you really mean "stop the action" instead of "stop reading on that file becuase end-of-file was reached" then you can't use Ctrl-D for that, it doesn't send any signal to the program. Use Ctrl-C instead.
Jeremy
Gnu/Vince
12-31-2001, 11:36 AM
Thanks Jeremy. I found a little bit of info about the signal module, but I haven't been able to use in a program yet though.
Gnu/Vince
12-31-2001, 12:17 PM
OK, i've got it for the signal. FOr those interested:
#!/usr/bin/env python
import sys, math, signal
def plural(n):
if n <= 1:
return ""
else:
return "s"
def exit(*args):
print "Interrupted by user. Exiting."
sys.exit(0)
def fact(target):
limit = math.sqrt(target)
nbr_of_facts = 0
for i in xrange(1, limit+1):
if target % i == 0:
print i, "and", target/i
nbr_of_facts += 1
return nbr_of_facts
def main():
signal.signal(signal.SIGINT, exit)
if len(sys.argv) < 2:
print "Usage: %s <Integer>" % sys.argv[0]
sys.exit(1)
try:
target = long(sys.argv[1])
except:
print "Illegal Input. Please use integers only!"
sys.exit(1)
nbr_of_facts = fact(target)
print "%d pair%s of factors found" % (nbr_of_facts, plural(nbr_of_facts))
if __name__ == '__main__':
main()