A little disappointed so far

Bengt Richter bokr at oz.net
Mon May 19 21:08:41 EDT 2003


On 19 May 2003 02:58:49 -0500, Ian Bicking <ianb at colorstudy.com> wrote:

>On Mon, 2003-05-19 at 02:38, Alex Martelli wrote:
>> >> open ($file) || die "Couldn't open $file"
>> >> strikes me as rather readable. And concise.
>> > 
>> > In Python, just write:
>> > f = open(file)
>> > 
>> > If there is an exception, an error message will be generated explaining
>> > why the file could not be opened and your script will terminate.
>> > 
>> > The problem with your Perl code is that it doesn't really help with
>> > problem diagnosis i.e. does the file not exist, is it a directory, do I
>> > not have the necessary permissions, etc.
>> 
>> That's just because the die statement's argument above isn't using the
>> normal idiomatic Perl form, which would be:
>> 
>>     die "Couldn't open data file $file: $!"
>> 
>> where the $! gives the details of the error.  Not Perl's fault for
>> once -- it's just as possible to erroneously and anti-idiomatically
>> omit printing the error details in a Python except clause.
>
>You have to go to more trouble with the Python, like:
>
>try:
>    f = open(file)
>except IOError, e:
>    print "Couldn't open %s" % file
>    # Equivalent improvement:
>    # print "Couldn't open %s: %s" % (file, e)
>
You can do it pretty generically if you just want a message. The default str(e)
is not so bad if you put the exception name in front:

 >>> def foo(exp):
 ...     try:
 ...         exec exp
 ...     except Exception, e:
 ...         print '%s: %s' % (e.__class__.__name__, e)
 ...         for name in [x for x in dir(e) if not x.startswith('__')]:
 ...             print '%12s: %r' % (name, getattr(e,name,'??'))
 ...
 >>> foo('f=file("zzz")')
 IOError: [Errno 2] No such file or directory: 'zzz'
         args: (2, 'No such file or directory')
        errno: 2
     filename: 'zzz'
     strerror: 'No such file or directory'
 >>> foo('1/0')
 ZeroDivisionError: integer division or modulo by zero
         args: ('integer division or modulo by zero',)
 >>> foo('1./0')
 ZeroDivisionError: float division
         args: ('float division',)
 >>> foo('[][1]')
 IndexError: list index out of range
         args: ('list index out of range',)

Hm, wonder what the special attributes are for all the exceptions...

 >>> import exceptions
 >>> for ex in dir(exceptions):
 ...     if ex.startswith('_'): continue
 ...     try:
 ...         exs = 'raise %s' % ex
 ...         exec exs
 ...     except Exception, e:
 ...         print '%s: %s'%(e.__class__.__name__, [x for x in dir(e) if not x.startswith('_')])
 ...
 ArithmeticError: ['args']
 AssertionError: ['args']
 AttributeError: ['args']
 DeprecationWarning: ['args']
 EOFError: ['args']
 EnvironmentError: ['args', 'errno', 'filename', 'strerror']
 Exception: ['args']
 FloatingPointError: ['args']
 IOError: ['args', 'errno', 'filename', 'strerror']
 ImportError: ['args']
 IndentationError: ['args', 'filename', 'lineno', 'msg', 'offset', 'print_file_and_line', 'text']   
 IndexError: ['args']
 KeyError: ['args']
 KeyboardInterrupt: ['args']
 LookupError: ['args']
 MemoryError: ['args']
 NameError: ['args']
 NotImplementedError: ['args']
 OSError: ['args', 'errno', 'filename', 'strerror']
 OverflowError: ['args']
 OverflowWarning: ['args']
 ReferenceError: ['args']
 RuntimeError: ['args']
 RuntimeWarning: ['args']
 StandardError: ['args']
 StopIteration: ['args']
 SyntaxError: ['args', 'filename', 'lineno', 'msg', 'offset', 'print_file_and_line', 'text']
 SyntaxWarning: ['args']
 SystemError: ['args']
 SystemExit: ['args', 'code']
 TabError: ['args', 'filename', 'lineno', 'msg', 'offset', 'print_file_and_line', 'text']
 TypeError: ['args']
 UnboundLocalError: ['args']
 UnicodeError: ['args']
 UserWarning: ['args']
 ValueError: ['args']
 Warning: ['args']
 WindowsError: ['args', 'errno', 'filename', 'strerror']
 ZeroDivisionError: ['args']

Hm no more ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list