for...else

2015-06-02 Thread acdr
Hi,

Currently, in various places in my code, I have the equivalent of:

for x in it:
if complicated_calculation_1():
cleanup()
break
complicated_calculation_2()
if complicated_calculation_3():
cleanup()
break

Obviously, I'm repeating myself by having two separate calls to
cleanup(). I can't really see a nicer way to do this. (Though I see
plenty of non-nice ways to do this, such as adding "broken = True" in
front of every "break", and then after the loop is done, have an "if
broken" section.) Other solutions that I'm not particularly fond of
can be found on stackexchange, where someone else is trying to do the
same thing:
http://stackoverflow.com/questions/3296044/opposite-of-python-for-else

I'm wondering if there is a demand for expanding the "for...else"
functionality to be expanded also have a block of code that only gets
called if the loop is broken out of. I.e.:

for x in it:
...
then:
# "break" was called
...
else:
# "break was not called
...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for...else

2015-06-02 Thread acdr
That would work for my example, but it would only really work if all
the calculations are in a nice function. If I just have a block of
code, then I can't use it as easily. (E.g. replace
"complicated_calculation_2()" by multiple lines of code, which I don't
want to shove into a separate function.)

On 6/2/15, Jean-Michel Pichavant  wrote:
> - Original Message -
>> From: "acdr" 
>> To: python-list@python.org
>> Sent: Tuesday, 2 June, 2015 1:26:42 PM
>> Subject: for...else
>>
>> Hi,
>>
>> Currently, in various places in my code, I have the equivalent of:
>>
>> for x in it:
>> if complicated_calculation_1():
>> cleanup()
>> break
>> complicated_calculation_2()
>> if complicated_calculation_3():
>> cleanup()
>> break
>
> Hi
>
> With the following layout, adding calculation steps is just a matter of
> adding a line
>
> for x ion it:
>   for calc, cbk in [
> (complicated_calculation_1, cleanup),
> (complicated_calculation_2, None),
> (complicated_calculation_3, cleanup),
>   ]:
> if calc() and cbk:
>   cbk()
>   break
>
> It might give you ideas.
>
> JM
>
>
> -- IMPORTANT NOTICE:
>
> The contents of this email and any attachments are confidential and may also
> be privileged. If you are not the intended recipient, please notify the
> sender immediately and do not disclose the contents to any other person, use
> it for any purpose, or store or copy the information in any medium. Thank
> you.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for...else

2015-06-02 Thread acdr
The first solution in your e-mail (with a Cleanup exception) is
definitely very close, functionally, to what I want to accomplish. In
effect, it's the same structure as my original suggestion of
"for...then...else", except now it'd be "try: for...else...except".
That's workable. I can even cheat and rename the Cleanup class to
"Break" for clarity.

I'm guessing that there is not much support for functionality like
this to be built in, in a much less verbose manner, like the
"for...else" functionality? :P

On 6/2/15, Jean-Michel Pichavant  wrote:
> - Original Message -
>> From: "acdr" 
>> To: "Jean-Michel Pichavant" 
>> Cc: python-list@python.org
>> Sent: Tuesday, 2 June, 2015 2:52:21 PM
>> Subject: Re: for...else
>>
>> That would work for my example, but it would only really work if all
>> the calculations are in a nice function.
>
> You cannot blame me for considering the example you provided ;)
>
> What about this:
>
> class Cleanup(Exception): pass
>
> try:
>   for x in it:
> if c1():
>   raise Cleanup()
> c2()
> if c3():
>   raise Cleanup()
> except Cleanup:
>   #do the cleanup
>pass
>
> If you can make c1 c3 raise themselves Cleanup, it's even better.
>
> Now if you're able to write a cleanup function that can handle the case when
> there's nothing to clean, everything becomes crystal clear:
>
> from contextlib import contextmanager
>
> @contextmanager
> def cleanup():
>   yield
>   # here do the cleaning
>   print 'I am cleaning'
>
> def do_the_job():
>   if c1() : return
>   c2()
>   if c3() : return
>
> with cleanup():
>   do_the_job()
>
> JM
>
>
> -- IMPORTANT NOTICE:
>
> The contents of this email and any attachments are confidential and may also
> be privileged. If you are not the intended recipient, please notify the
> sender immediately and do not disclose the contents to any other person, use
> it for any purpose, or store or copy the information in any medium. Thank
> you.
>
-- 
https://mail.python.org/mailman/listinfo/python-list