List Info

Thread: for loops and exceptions




for loops and exceptions
user name
2006-03-29 18:56:31
Hello,

>From a general style and/or programmatic perspective,
which is a "better"
way to write this bit of code?

try:
    (dn, attrs) = conn.search_s(search_base, search_scope,
search_filter, search_attrs):
except Exception, e:
        warn_the_user(e)
        do_something_useful()

for (name, data) in (dn, attrs):
        print '%s' % (name)
        for key, value in data.iteritems():
                print '%s => %s' % (key, value)

OR

try:
    for dn, attrs in conn.search_s(search_base,
search_scope, search_filter, search_attrs):
        print dn
        for key, value in attrs.iteritems():
            print '\t%s => %s' % (key, value)
        print
except Exception, e:
     warn_the_user(e)
     do_something_useful


Personally I like the second method more.  To my eyes it is
compact
and efficient.  Coming from a perl background I tend to like
to get
very compact, which can be an impediment to code
readability.  As I get
started with python I want to make sure that I don't repeat
the same
mistakes and let too many perl-isms creep into my python
code.

-mtw


-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where
we are, but in
what direction we are moving."   -Oliver Wendell
Holmes

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
for loops and exceptions
user name
2006-03-30 13:21:56
Matthew White wrote:
> Hello,
> 
>>From a general style and/or programmatic
perspective, which is a "better"
> way to write this bit of code?

Hmm, neither?
> 
> try:
>     (dn, attrs) = conn.search_s(search_base,
search_scope, search_filter, search_attrs):
> except Exception, e:
>         warn_the_user(e)
>         do_something_useful()
> 
> for (name, data) in (dn, attrs):
>         print '%s' % (name)
>         for key, value in data.iteritems():
>                 print '%s => %s' % (key, value)

This will run the for loop even if you get an exception. I
think the for 
loop is buggy, too, it is different from the one below.
> 
> OR
> 
> try:
>     for dn, attrs in conn.search_s(search_base,
search_scope, search_filter, search_attrs):
>         print dn
>         for key, value in attrs.iteritems():
>             print '\t%s => %s' % (key, value)
>         print
> except Exception, e:
>      warn_the_user(e)
>      do_something_useful

This might be OK. It will catch exceptions in the for loop,
which you 
may or may not want.

If this code is at a low level of your program, it's best
to only catch 
expected exceptions, and let unexpected exceptions propagate
to a higher 
level. At the higher level, you can have a generic except
block that 
reports the error and moves on. This is often in a top-level
loop or 
event handler. Your code has elements of both - code that
does real work 
with a generic except block that reports errors.

Anyway here is a construction that is very useful for
catching just the 
exceptions you expect to see from a bit of code, while
running the rest 
of the code outside of the try block.

try:
     (dn, attrs) = conn.search_s(search_base, search_scope, 
search_filter, search_attrs):
except ConnectionError, e:
         warn_the_user(e)
         do_something_useful()
else:
         print dn
         for key, value in attrs.iteritems():
             print '\t%s => %s' % (key, value)
         print

The idea is to put as little code as possible in the scope
of the try, 
and to catch as few exceptions as possible. If no exception
is raised, 
the else: block will run. If there is an exception in that
code, it will 
propagate to a higher level.

Kent

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )