439,931 Members | 2,015 Online
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

 P: n/a 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 -- Neil Cerutti We've got to pause and ask ourselves: How much clean air do we really need? --Lee Iacocca Jan 10 '07 #1
5 Replies

 P: n/a On 2007-01-10, Fredrik Lundh

 P: n/a Neil Cerutti wrote: 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/Coo.../Recipe/304373 George Jan 10 '07 #3

 P: n/a Neil Cerutti wrote: 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/Coo.../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 Jan 10 '07 #4

 P: n/a On 2007-01-10, Steven Bethard For use in a hand-coded parser I wrote the following simpleiterator with look-ahead. There's a recipe for this: http://aspn.activestate.com/ASPN/Coo.../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 returnwhen the iterator is exhausted. Suggestions are respectfullyrequested. 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. -- Neil Cerutti Jan 10 '07 #5

 P: n/a Neil Cerutti wrote: On 2007-01-10, Steven Bethard

### This discussion thread is closed

Replies have been disabled for this discussion.