try -> except -> else -> except?

2009-07-06 Thread David House
Hi all,

I'm looking for some structure advice. I'm writing something that
currently looks like the following:

try:

except KeyError:

else:


This is working fine. However, I now want to add a call to a function
in the `else' part that may raise an exception, say a ValueError. So I
was hoping to do something like the following:

try:

except KeyError:

else:

except ValueError:


However, this isn't allowed in Python.

An obvious way round this is to move the `else' clause into the `try', i.e.,

try:


except KeyError:

except ValueError:


However, I am loath to do this, for two reasons:

(i) if I modify the  block at some point in
the future so that it may raise a KeyError, I have to somehow tell
this exception from the one that may be generated from the  line.
(ii) it moves the error handler for the  bit miles away from the line that might generate the
error, making it unclear which code the KeyError error handler is an
error handler for.

What would be the best way to structure this?

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


Re: try -> except -> else -> except?

2009-07-06 Thread David House
2009/7/6 Python :
> as far as I know try has no 'else'

It does:
http://docs.python.org/reference/compound_stmts.html#the-try-statement

> it's 'finally'

There is a `finally', too, but they are semantically different. See
the above link.

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


Re: what is it, that I don't understand about python and lazy evaluation?

2009-08-13 Thread David House
2009/8/13 Erik Bernoth :
> after 14 it is not nessesary to evaluate evens() any further.

How does Python know this? I.e. how does it know that evens() will
always yield things in ascending order? For example, I could write an
iterator like this:

def my_iter():
for i in [0,2,4,6,8,10,12,14,16,18,1,3,5]:
yield i

Now, imagine I do [i for i in my_iter() if i < 15]. If you quit
iterating after `i' becomes 16, you'll miss the valid numbers 1, 3, 5
at the end of the list!

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


Re: ignored test cases in unittest

2009-08-17 Thread David House
2009/8/16 Terry :
> Thanks for the solutions. I think the decorator idea is what I'm look
> for:-)

Note that the unittest module now supports the `skip' and
`expectedFailure' decorators, which seem to describe some of the
solutions here.

See 
http://docs.python.org/3.1/library/unittest.html#skipping-tests-and-expected-failures

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


Re: [OT] How do I reply to a thread by sending a message to python-list@python.org

2009-08-27 Thread David House
2009/8/27 Terry Reedy :
> reply-all may send duplicate messages to the author. Not sure of this list.

I'm fairly sure Mailman deals with that.

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