Re: How to terminate a main script?

2006-07-12 Thread Tal Einat
Helmut Jarausch wrote:
> Hi,
>
> I'm still looking for an elegant and clear means to
> terminate the main script in Python.
>
> Unfortunately, Python doesn't allow a 'return'
> instruction in the main script.
>
It is quite a common practice for Python scripts to define a main()
function which contains the actual code, and have "main()" on the last
line of the file. This allows you to just 'return' from wherever you
like in your code.

It is even more common to use this in the end instead of "main()":
if __name__ == "__main__":
main()

This way, if the file is imported as a module, the main() function
won't execute, but if it is run directly from a command line or using
execfile() it will execute.

Enjoy!

- Tal

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to terminate a main script?

2006-07-11 Thread Helmut Jarausch
Fredrik Lundh wrote:
> Helmut Jarausch wrote:
> 
>> Using sys.exit(0) produces an error
>> message which looks dangerous to an
>> uninitiated user.
> 
> sys.exit(0) doesn't print anything at all.

Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
   File "", line 1, in -toplevel-
 sys.exit(0)
SystemExit: 0


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to terminate a main script?

2006-07-11 Thread Ganesan Rajagopal
> Helmut Jarausch <[EMAIL PROTECTED]> writes:

> Using sys.exit(0) produces an error message which looks dangerous to an
> uninitiated user.

What message? Your program should exit silently when you call sys.exit(0). 

Ganesan

-- 
Ganesan Rajagopal

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to terminate a main script?

2006-07-11 Thread Fredrik Lundh
Helmut Jarausch wrote:

> Using sys.exit(0) produces an error
> message which looks dangerous to an
> uninitiated user.

sys.exit(0) doesn't print anything at all.

$ python
>>> import sys
>>> sys.exit(0)
$

however, sys.exit() raises an exception to tell the runtime that it wants
to terminate the program, so if you're using a catch-all exception handler
that treats all exceptions as dangerous errors, it'll look like a dangerous
error too.

the solution is simple: don't do that.  instead of writing:

try:
...
except:
print "OMG! Ponies!"

write

try:
...
except (SystemExit, KeyboardInterupt):
raise
except:
print "OMG! Ponies!"

 



-- 
http://mail.python.org/mailman/listinfo/python-list


How to terminate a main script?

2006-07-11 Thread Helmut Jarausch
Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.

Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

The same is true for an exception.

And setting a 'flag' and testing
at several place for 'fall through'
is ugly and error-prone.

So what is a good choice?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list