Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid StopIteration Error in python

I have a line that is pulling in variables from multiple lists and I want it to avoid the StopIteration error that comes up so that it can move onto the next line. At the moment I am using the break function, this avoids the StopIteration, but only gives me the first item in the list and it leaves a blank line after it, if i was to print it out.

Here are two of my iterations that have the same problem.

def compose_line5(self, synset_offset, pointer_list):
    self.line5 = ''''''
    for item in pointer_list:
        self.line5 += '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+pointer_list.next()+'''\n'''            
        break
    return self.line5

def compose_line6(self, pointer_list, synset_list): 
    self.line6 = ''''''
    for item in synset_list:
        self.line6 += '''http://www.example.org/lexicon#'''+pointer_list.next()+''' http://www.monnetproject.eu/lemon#pos '''+synset_list.next()+'''\n'''                      
        break
    return self.line6

This is the error I get without the break:

Traceback (most recent call last):
  File "wordnet.py", line 225, in <module>
    wordnet.line_for_loop(my_file)
  File "wordnet.py", line 62, in line_for_loop
    self.compose_line5(self.synset_offset, self.pointer_list)
  File "wordnet.py", line 186, in compose_line5
    self.line5 += '''http://www.example.org/lexicon#'''+self.synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+self.pointer_list.next()+'''\n'''
StopIteration

Is there a quick fix for this or do I have to catch exceptions for every method I use the iter() in?

like image 493
JohnConneely Avatar asked Jun 26 '13 10:06

JohnConneely


People also ask

What is StopIteration Python?

StopIteration in PythonThe StopIteration can be used in the next() method to prevent the iteration after a specific condition or termination statement. Syntax is. class MyNumbers: def __iter__(self):

What are the methods that iterator object must implement?

Technically speaking, a Python iterator object must implement two special methods, __iter__() and __next__() , collectively called the iterator protocol. An object is called iterable if we can get an iterator from it. Most built-in containers in Python like: list, tuple, string etc. are iterables.

How do I use an iterator in Python?

Create an IteratorTo create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object. As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__() , which allows you to do some initializing when the object is being created.


1 Answers

In compose_line5, instead of pointer_list.next(), use item - you are already iterating over pointer_list.

For compose_line6, it seems you want to iterate over two lists at the same time. Use the top answer from Is there a better way to iterate over two lists, getting one element from each list for each iteration? (I'm assuming both lists are the same length)

Yes, the iterator protocol will raise StopIteration (not an error, just an exception signalling the end of the iteration) if you call .next() on it manually. The pythonic way to use it is to use it as a normal iterator (for example, looping over it) and not call .next() on it.

Your code has a few issues beyond that that you may want to look at - look at http://www.python.org/dev/peps/pep-0008/

For example, no need to use '''''' when '' suffices. Instead of doing +=, you may want to create a list then join in the end. Not sure why you store things in self if you're just returning them from the function.

like image 152
Thomas Vander Stichele Avatar answered Oct 17 '22 08:10

Thomas Vander Stichele