Capture StopIteration Error Message in For Loop - python

I have code similar to this structure:
def my_gen(some_str):
if some_str == "":
raise StopIteration("Input was empty")
else:
parsed_list = parse_my_string(some_str)
for p in parsed_list:
x, y = p.split()
yield x, y
for x, y in my_gen()
# do stuff
# I want to capture the error message from StopIteration if it was raised manually
Is it possible to do this by using a for loop? I couldn't find a case similar to this elsewhere.
If using a for loop isn't possible, what are some other alternatives?
Thanks

You cannot do this in a for loop - because a for loop will implicitly catch the StopIteration exception.
One possible way to do this is with an infinite while:
while True:
try:
obj = next(my_gen)
except StopIteration:
break
print('Done')
Or you can use any number of consumers from the itertools library - have a look at the recipe section at the bottom for examples.

Related

Can I save results anyway even when Keyboardinterrupt?

I have a very long code which is taking forever to run. I was wondering if there is a way to save the results even if I use the keyboard to interrupt the code from running? All the examples I found were using except with Keyboardinterrupt, so I don't know if this is the right code to use.
More concretely: I have a code which ends with saving results in a list, and returning the list. In this case, is there a way to return the list despite keyboardinterrupt? Can I use if keyboardinterrupt statement?
My code:
# removed is a very long list
for a, b in itertools.combinations(removed, 2):
temp = [a,b]
Token_Set_Ratio = fuzz.token_set_ratio(temp[0],temp[1])
if Token_Set_Ratio > k:
c = random.choice(temp)
if c in removed:
removed.remove(c)
else:
pass
else:
pass
return removed
Where can I add the part for python to retain removed even if keyboard interrupt occurs?
You could use a try-except with KeyboardInterrupt:
def your_function():
removed = [...]
try:
# Code that takes long time
for a, b in itertools.combinations(removed, 2):
...
return removed
except KeyboardInterrupt:
return removed
A small example:
import time
def foo():
result = []
try:
# Long running code
for i in range(10000):
result.append(i)
time.sleep(0.1)
return result
except KeyboardInterrupt:
# Code to "save"
return result
print(foo())
When you Ctrl-C before the end of execution, a partial list is printed.

Using try-excepts in nested for loops

I have a nested loop where I perform a calculation over every grid-point in a 3-dimensional array. Some of these grid-points trigger an error. I want to calculate every possible grid-point and skip those that trigger the error.
I've tried:
with sharedmem.MapReduce(np=45) as pool:
def calc_func(abc):
for k in range(241):
try:
for j in range(int(datetime_range)):
for l in range((abc), abc+1):
value = calc(pr[j,k,l], te[j,k,l], de[j,k,l])
array[j,k,l] = value
except (IndexError, ValueError):
continue
pool.map(cape_func, range(0, 479))
^ Above, some grid-points are calculated, but I think when k values are caught in the exception, the code doesn't calculate the rest of the grid-points for that k.
Below, I tried adding another try-except block to iterate over all of the grid-points. I don't think this worked - it seems to be looping infinitely.
with sharedmem.MapReduce(np=45) as pool:
def calc_func(abc):
for k in range(241):
try:
for j in range(int(datetime_range)):
try:
for l in range((abc), abc+1):
value = calc(pr[j,k,l], te[j,k,l], de[j,k,l])
array[j,k,l] = value
except (IndexError, ValueError):
continue
except (IndexError, ValueError):
continue
pool.map(cape_func, range(0, 479))
I think you might be looking for a pass statement instead of a continue. But I might not have understood your questions well. On the side note, can you not vectorise it?
It also looks like the only thing that could throw Index or Value errors is the calc function, since all your loop are over a ranges. Why don't you just wrap the 2 code lines with the function and array assignment?

Why must I use a variable to get values from a Python generator?

Why must I use a variable to obtain next() values from a Python generator?
def my_gen():
i = 0
while i < 4:
yield 2 * i
i += 1
#p = my_gen()
#for i in range(4):
# print(next(p))
##for i in range(4):
## print(next(my_gen()))
In the above, the # block works, while the ## block returns 4 copies of the first "yield."
print(next(my_gen()))
Each time this runs, you're calling next() on a separate (and brand-new) generator returned by my_gen().
If you want to call next() on the same generator more than once, you'll need some way to keep that generator around for long enough to reuse it.
just gonna add my $0.02...
this may help clear up some misconceptions:
def function_that_creates_a_generator(n):
while n>0:
yield n
n - 1
# this
for x in function_that_creates_a_generator(5):
print(x)
#is almost exactly the same as this
generator = function_that_creates_a_generator(5)
while True:
try:
x = generator.next() #same as: x = next(generator)
except StopIteration:
break
print(x)
For loops are really just a prettier way of writing a while loop that continually asks an iterable object for its next element. when you ask an iterable for another object and it has none, it will raise a StopIteration exception that is automatically caught in for loops; signaling a break from the loop. In the while loop we simply break these things out individually instead of having them hidden by the interpreter.

Redo for loop iteration in Python

Does Python have anything in the fashion of a "redo" statement that exists in some languages?
(The "redo" statement is a statement that (just like "break" or "continue") affects looping behaviour - it jumps at the beginning of innermost loop and starts executing it again.)
No, Python doesn't have direct support for redo. One option would something faintly terrible involving nested loops like:
for x in mylist:
while True:
...
if shouldredo:
continue # continue becomes equivalent to redo
...
if shouldcontinue:
break # break now equivalent to continue on outer "real" loop
...
break # Terminate inner loop any time we don't redo
but this mean that breaking the outer loop is impossible within the "redo-able" block without resorting to exceptions, flag variables, or packaging the whole thing up as a function.
Alternatively, you use a straight while loop that replicates what for loops do for you, explicitly creating and advancing the iterator. It has its own issues (continue is effectively redo by default, you have to explicitly advance the iterator for a "real" continue), but they're not terrible (as long as you comment uses of continue to make it clear you intend redo vs. continue, to avoid confusing maintainers). To allow redo and the other loop operations, you'd do something like:
# Create guaranteed unique sentinel (can't use None since iterator might produce None)
sentinel = object()
iterobj = iter(mylist) # Explicitly get iterator from iterable (for does this implicitly)
x = next(iterobj, sentinel) # Get next object or sentinel
while x is not sentinel: # Keep going until we exhaust iterator
...
if shouldredo:
continue
...
if shouldcontinue:
x = next(iterobj, sentinel) # Explicitly advance loop for continue case
continue
...
if shouldbreak:
break
...
# Advance loop
x = next(iterobj, sentinel)
The above could also be done with a try/except StopIteration: instead of two-arg next with a sentinel, but wrapping the whole loop with it risks other sources of StopIteration being caught, and doing it at a limited scope properly for both inner and outer next calls would be extremely ugly (much worse than the sentinel based approach).
No, it doesn't. I would suggest using a while loop and resetting your check variable to the initial value.
count = 0
reset = 0
while count < 9:
print 'The count is:', count
if not someResetCondition:
count = count + 1
This is my solution using iterators:
class redo_iter(object):
def __init__(self, iterable):
self.__iterator = iter(iterable)
self.__started = False
self.__redo = False
self.__last = None
self.__redone = 0
def __iter__(self):
return self
def redo(self):
self.__redo = True
#property
def redone(self):
return self.__redone
def __next__(self):
if not (self.__started and self.__redo):
self.__started = True
self.__redone = 0
self.__last = next(self.__iterator)
else:
self.__redone += 1
self.__redo = False
return self.__last
# Display numbers 0-9.
# Display 0,3,6,9 doubled.
# After a series of equal numbers print --
iterator = redo_iter(range(10))
for i in iterator:
print(i)
if not iterator.redone and i % 3 == 0:
iterator.redo()
continue
print('---')
Needs explicit continue
redone is an extra feature
For Python2 use def next(self) instead of def __next__(self)
requires iterator to be defined before the loop
I just meet the same question when I study perl,and I find this page.
follow the book of perl:
my #words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
my #words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
foreach (#words){
print "Type the word '$_':";
chomp(my $try = <STDIN>);
if ($try ne $_){
print "Sorry - That's not right.\n\n";
$error++;
redo;
}
}
and how to achieve it on Python ??
follow the code:
tape_list=['a','b','c','d','e']
def check_tape(origin_tape):
errors=0
while True:
tape=raw_input("input %s:"%origin_tape)
if tape == origin_tape:
return errors
else:
print "your tape %s,you should tape %s"%(tape,origin_tape)
errors += 1
pass
all_error=0
for char in tape_list:
all_error += check_tape(char)
print "you input wrong time is:%s"%all_error
Python has not the "redo" syntax,but we can make a 'while' loop in some function until get what we want when we iter the list.
Not very sophiscated but easy to read, using a while and an increment at the end of the loop. So any continue in between will have the effect of a redo. Sample to redo every multiple of 3:
redo = True # To ends redo condition in this sample only
i = 0
while i<10:
print(i, end='')
if redo and i % 3 == 0:
redo = False # To not loop indifinively in this sample
continue # Redo
redo = True
i += 1
Result: 00123345667899
There is no redo in python.
A very understandable solution is as follow:
for x in mylist:
redo = True
while redo:
redo = False
If should_redo:
redo = True
It's clear enough to do not add comments
Continue will work as if it was in the for loop
But break is not useable, this solution make break useable but the code is less clear.
Here is a solution for python 3.8+ since now we have the := operator:
for key in mandatory_attributes: # example with a dictionary
while not (value := input(f"{key} (mandatory): ")):
print("You must enter a value")
mandatory_attributes[key] = value

break and continue in function

def funcA(i):
if i%3==0:
print "Oh! No!",
print i
break
for i in range(100):
funcA(i)
print "Pass",
print i
I know script above won't work. So, how can I write if I need put a function with break or continue into a loop?
A function cannot cause a break or continue in the code from which it is called. The break/continue has to appear literally inside the loop. Your options are:
return a value from funcA and use it to decide whether to break
raise an exception in funcA and catch it in the calling code (or somewhere higher up the call chain)
write a generator that encapsulates the break logic and iterate over that instead over the range
By #3 I mean something like this:
def gen(base):
for item in base:
if item%3 == 0:
break
yield i
for i in gen(range(1, 100)):
print "Pass," i
This allows you to put the break with the condition by grouping them into a generator based on the "base" iterator (in this case a range). You then iterate over this generator instead of over the range itself and you get the breaking behavior.
Elaborating BrenBarns answer: break fortunately will not propagate. break is to break the current loop, period. If you want to propagate an event, then you should raise an exception. Although, raising the exception to break the loop is a really ugly way to break loops and a nice way to break your code.
KISS! The simplest would be to check the condition directly in the loop
def my_condition(x):
return x == 4
for i in xrange(100):
if my_condition(i): break
print i
If, for some reason, you want to propagate an exception, then you use it like this
# exception example
for i in xrange(100):
if i == 4: raise Exception("Die!")
print i
As mentioned, it is a really ugly design. Imagine you forget to catch this exception, or you change its type from Exception to MyBreakException and forget to change it somewhere in try/except higher part of the code...
The generator example has its merits, it makes your code more functional style (which I presonally adore)
# generator example
def conditional_generator(n, condition):
for i in xrange(n):
if condition(i):
break
else:
yield i
for i in conditional_generator( 100, my_condition ):
print i
...which is similar to takewhile, mentioned by eumiro
def funcA(i):
if i%3==0:
print "Oh! No!",
print i
return True
else:
return False
for i in range(100):
if funcA(i):
break
print "Pass",
print i
Break won't propagate between functions, you need to put it directly within the loop somewhere.

Categories