An iterator with look-ahead

N

Neil Cerutti

For use in a hand-coded parser I wrote the following simple
iterator with look-ahead. I haven't thought too deeply about what
peek ought to return when the iterator is exhausted. Suggestions
are respectfully requested. As it is, you can't be sure what a
peek() => None signifies until the next iteration unless you
don't expect None in your sequence.

Using itertools.tee is the alternative I thought about, but
caveates in the documentation disuaded me.

class LookAheadIter(object):
""" An iterator with the a peek() method, so you can see what's coming next.

If there is no look-ahead, peek() returns None.
>>> nums = [1, 2, 3, 4, 5]
>>> look = LookAheadIter(nums)
>>> for a in look:
... print (a, look.peek())
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, None)
"""
def __init__(self, data):
self.iter = iter(data)
self.look = self.iter.next()
self.exhausted = False
def __iter__(self):
return self
def peek(self):
if self.exhausted:
return None
else:
return self.look
def next(self):
item = self.look
try:
self.look = self.iter.next()
except StopIteration:
if self.exhausted:
raise
else:
self.exhausted = True
return item
 
G

George Sakkis

Neil said:
For use in a hand-coded parser I wrote the following simple
iterator with look-ahead. I haven't thought too deeply about what
peek ought to return when the iterator is exhausted. Suggestions
are respectfully requested. As it is, you can't be sure what a
peek() => None signifies until the next iteration unless you
don't expect None in your sequence.

There is a different implementation in the Cookbook already:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

George
 
S

Steven Bethard

Neil said:
For use in a hand-coded parser I wrote the following simple
iterator with look-ahead.

There's a recipe for this:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

Note that the recipe efficiently supports an arbitrary look-ahead, not
just a single item.
I haven't thought too deeply about what peek ought to return
when the iterator is exhausted. Suggestions are respectfully
requested.

In the recipe, StopIteration is still raised on a peek() operation that
tries to look past the end of the iterator.

STeVe
 
N

Neil Cerutti

There's a recipe for this:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

Note that the recipe efficiently supports an arbitrary
look-ahead, not just a single item.


In the recipe, StopIteration is still raised on a peek()
operation that tries to look past the end of the iterator.

That was all I could think of as an alternative, but that makes
it fairly inconvenient to use. I guess another idea might be to
allow user to provide a "no peek" return value in the
constructor, if they so wish.
 
P

Paddy

Neil said:
That was all I could think of as an alternative, but that makes
it fairly inconvenient to use. I guess another idea might be to
allow user to provide a "no peek" return value in the
constructor, if they so wish.
You could raise a different Exception, PeekPastEndEception ?
- Paddy.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top