Python tricks

R

RajNewbie

Hi,
My code has a lot of while loops of the following format:
while True:
...
if <condition>: break

The danger with such a code is that it might go to an infinite loop
- if the <condition> never occurs.
Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?

I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break

But I am not very happy with this code for 3 reasons
1. Verbosity (i=0 and i++) which doesnt add to the logic
2. The loop now has dual focus. - incrementing i, etc.
3. <most important> A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.

The solution that I had in mind is:
while True:
...
if <condition>: break
if inifinte_loop(): raise infiinte_loop_exception

Wherein infinite_loop is a generator, which returns true if i > 200
def infinite_loop():
i = 0
while i < 200:
i++
yield False
yield True

Could somebody let me know whether this is a good option?
One of my main worries is - what will happen if I call this same
procedure from another loop? Will it start again from 0 or will it
again start from my previous stored i? i.e.
def big_proc:
while True:
...
if infinite_loop: raise

while True:
...
if infinite_loop: raise
In such a case, will we run it 200 times both the times or not?

Could someone chip in with other suggestions?
 
T

Tim Chase

My code has a lot of while loops of the following format:
while True:
...
if <condition>: break

The danger with such a code is that it might go to an infinite loop
- if the <condition> never occurs.
Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?

I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break

But I am not very happy with this code for 3 reasons
1. Verbosity (i=0 and i++) which doesnt add to the logic
2. The loop now has dual focus. - incrementing i, etc.
3. <most important> A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.

My first thought would be to simply not use "while True":

INFINITE_LOOP_COUNT = 200
for _ in xrange(INFINITE_LOOP_COUNT):
do_something()
if <condition>: break
else:
raise InfiniteLoopException
The solution that I had in mind is:
while True:
...
if <condition>: break
if inifinte_loop(): raise infiinte_loop_exception

Wherein infinite_loop is a generator, which returns true if i > 200
def infinite_loop():
i = 0
while i < 200:
i++
yield False
yield True

Could somebody let me know whether this is a good option?

To do this, you'd need to do the same sort of thing as you do
with your i/i++ variable:

i = infinite_loop()
while True:
...
if <condition>: break
if i.next(): raise InfiniteLoopException

which doesn't gain much, and makes it a whole lot more confusing.
Could someone chip in with other suggestions?

As an aside: the phrase is "chime in"[1] (to volunteer
suggestions) "Chip in"[2] usually involves contributing money to
a common fund ("care to chip in $10 for Sally's wedding gift from
the office?" where the pool of money would then be used to buy
one large/expensive gift for Sally)

-tkc


[1]
http://www.thefreedictionary.com/chime+in

[2]
http://www.english-test.net/forum/ftopic1768.html
 
R

RajNewbie

   My code has a lot of while loops of the following format:
   while True:
     ...
     if <condition>: break
   The danger with such a code is that it might go to an infinite loop
- if the <condition> never occurs.
   Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?
   I do understand that we can use the code like -
   i = 0
   while True:
     i++
     if i > 200: raise infinite_Loop_Exception
     ...
     if <condition>: break
   But I am not very happy with this code for 3 reasons
   1. Verbosity (i=0 and i++) which doesnt add to the logic
   2. The loop now has dual focus. - incrementing i, etc.
   3. <most important>   A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.

My first thought would be to simply not use "while True":

   INFINITE_LOOP_COUNT = 200
   for _ in xrange(INFINITE_LOOP_COUNT):
     do_something()
     if <condition>: break
   else:
     raise InfiniteLoopException
   The solution that I had in mind is:
   while True:
     ...
     if <condition>: break
     if inifinte_loop(): raise infiinte_loop_exception
  Wherein infinite_loop is a generator, which returns true if i > 200
  def infinite_loop():
     i = 0
     while i < 200:
         i++
         yield False
     yield True
Could somebody let me know whether this is a good option?

To do this, you'd need to do the same sort of thing as you do
with your i/i++ variable:

   i = infinite_loop()
   while True:
     ...
     if <condition>: break
     if i.next(): raise InfiniteLoopException

which doesn't gain much, and makes it a whole lot more confusing.
Could someone chip in with other suggestions?

As an aside:  the phrase is "chime in"[1] (to volunteer
suggestions) "Chip in"[2] usually involves contributing money to
a common fund ("care to chip in $10 for Sally's wedding gift from
the office?"  where the pool of money would then be used to buy
one large/expensive gift for Sally)

-tkc

[1]http://www.thefreedictionary.com/chime+in

[2]http://www.english-test.net/forum/ftopic1768.html

Thank you very much Tim.
I agree on all counts - esp the fact that my suggestion is very
confusing + (chime in part too :) ).
But, I still feel it would be much more aesthetically pleasing if I
can call a single procedure like
if infinite_loop() -> to do the same.
Is it somehow possible? - say by using static variables, iterators --
anything?
 
P

Paul Rubin

RajNewbie said:
I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break

But I am not very happy with this code for 3 reasons

I prefer:

from itertools import count

for i in count():
if i > 200: raise infinite_Loop_Exception
...

You could also use:

for i in xrange(200):
...
else:
raise infinite_Loop_Exception

The "else" clause runs only if no break statement is executed.
 
J

John Machin

Could someone chip in with other suggestions?

As an aside:  the phrase is "chime in"[1] (to volunteer
suggestions) "Chip in"[2] usually involves contributing money to
a common fund ("care to chip in $10 for Sally's wedding gift from
the office?"  where the pool of money would then be used to buy
one large/expensive gift for Sally)

All rather locale-dependent; see e.g. http://www.answers.com/topic/chip-in
 
R

Robert Latest

RajNewbie said:
Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?

I do understand that we can use the code like -
i = 0
while True:
i++
if i > 200: raise infinite_Loop_Exception
...
if <condition>: break

But I am not very happy with this code for 3 reasons
1. Verbosity (i=0 and i++) which doesnt add to the logic
2. The loop now has dual focus. - incrementing i, etc.
3. <most important> A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.

Maybe you should call the counter variable something meaningful instead
of -- of all things -- "i", which is idiomatic for soething entirely
else. And add a comment, and be done with it.
The solution that I had in mind is:
while True:
...
if <condition>: break
if inifinte_loop(): raise infiinte_loop_exception

This is a lot less understandable because whoever is working with your
code will now have to check an additional function rather than a
pretty self-explanatory variable increment.

robert
 
R

Robert Latest

RajNewbie said:
But, I still feel it would be much more aesthetically pleasing if I
can call a single procedure like
if infinite_loop() -> to do the same.

You may find it aesthetically pleasing, and it may very well be, but it
will obfuscate your code and make it less maintainable.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top