Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Syntactic structure for 'until <Exception>:' loop

Reply
Thread Tools

Syntactic structure for 'until <Exception>:' loop

 
 
eblume
Guest
Posts: n/a
 
      01-12-2011
I'm still quite new to Python and I'm probably going about this
entirely the wrong way, but it recently struck me that there might be
the need for a control flow loop based on exception handling. First
let me give the proposed syntax:

until <Exception[, Exception]...>:
do_something()

This would be exactly equivalent to (but much more compact than):

while True:
try:
do_something()
except Exception:
break

Now, why would anyone want this structure? In my case, I'm using it
(well, the latter form of it, obviously) to loop over an iterator
object that was not created via the 'for obj in collection:' syntax.
Here's the actual code snippet:

headers = self.reader.next()
... intermediate code ....
while True:
try:
line = self.reader.next()
except StopIteration:
return data
data.append(line)

I'm sure I'm doing this in a very backward and wrong way, and would
appreciate tips on a better way to accomplish the same task. Obviously
there is an existing syntax which handles the same situations, and I
don't suspect that this will be an embraced proposal, I'm more hoping
to spark some conversation.

Thanks!
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      01-12-2011
On 1/11/2011 7:22 PM, eblume wrote:
> This would be exactly equivalent to (but much more compact than):
>
> while True:
> try:
> do_something()
> except Exception:
> break


Or perhaps:

try:
while True:
do_something()
except Exception:
pass

> Now, why would anyone want this structure? In my case, I'm using it
> (well, the latter form of it, obviously) to loop over an iterator
> object that was not created via the 'for obj in collection:' syntax.
> Here's the actual code snippet:
>
> headers = self.reader.next()
> ... intermediate code ....
> while True:
> try:
> line = self.reader.next()
> except StopIteration:
> return data
> data.append(line)
>
> I'm sure I'm doing this in a very backward and wrong way, and would
> appreciate tips on a better way to accomplish the same task. Obviously
> there is an existing syntax which handles the same situations, and I
> don't suspect that this will be an embraced proposal, I'm more hoping
> to spark some conversation.



reader_iter = iter(self.reader)
headers = reader_iter.next()
# intermediate code
for line in reader_iter:
data.append(line)
return data


Also note that recommended best practice is to wrap the "headers =
reader_iter.next()" line in a try-except in case it raises a
StopIteration. Otherwise it could get propagated silently up to some
unrelated for loop higher in the stack, resulting in unexpected behavior.

Cheers,
Ian

 
Reply With Quote
 
 
 
 
eblume
Guest
Posts: n/a
 
      01-12-2011
On Jan 11, 6:53*pm, Ian Kelly <(E-Mail Removed)> wrote:
> On 1/11/2011 7:22 PM, eblume wrote:
>
> > <snip>

>
> reader_iter = iter(self.reader)
> headers = reader_iter.next()
> # intermediate code
> for line in reader_iter:
> * * *data.append(line)
> return data
>
> Also note that recommended best practice is to wrap the "headers =
> reader_iter.next()" line in a try-except in case it raises a
> StopIteration. *Otherwise it could get propagated silently up to some
> unrelated for loop higher in the stack, resulting in unexpected behavior.
>
> Cheers,
> Ian


That's brilliant, exactly the code I was looking for. Thanks very much
Ian!

Erich

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-12-2011
Ian Kelly wrote:

> reader_iter = iter(self.reader)
> headers = reader_iter.next()
> # intermediate code
> for line in reader_iter:
> data.append(line)
> return data


If data is a list the for loop can be replaced with

data.extend(reader_iter)

or, if data is an empty list created within the function

data = list(reader_iter)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
syntactic sugar for initializaing multidimentional arrays? aaronfude@gmail.com C++ 1 01-24-2005 07:03 PM
Ruby like syntactic sugar F Jamitzky Python 6 03-04-2004 07:05 AM
xrange() syntactic sugar Christopher T King Python 0 01-09-2004 01:26 AM
Is -> syntactic sugar? Jason Heyes C++ 17 12-19-2003 05:49 PM



Advertisments