Re: An iterator with look-ahead
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. if you're doing simple parsing on an iterable, it's easier and more efficient to pass around the current token and the iterator's next method: http://online.effbot.org/2005_11_01_archive.htm#simple-parser-1 /F -- http://mail.python.org/mailman/listinfo/python-list
Re: An iterator with look-ahead
On 2007-01-10, Fredrik Lundh [EMAIL PROTECTED] wrote: if you're doing simple parsing on an iterable, it's easier and more efficient to pass around the current token and the iterator's next method: http://online.effbot.org/2005_11_01_archive.htm#simple-parser-1 Thank you. Much better. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: An iterator with look-ahead
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/Cookbook/Python/Recipe/304373 George -- http://mail.python.org/mailman/listinfo/python-list
Re: An iterator with look-ahead
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/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 -- http://mail.python.org/mailman/listinfo/python-list
Re: An iterator with look-ahead
On 2007-01-10, Steven Bethard [EMAIL PROTECTED] wrote: 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/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. 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 -- http://mail.python.org/mailman/listinfo/python-list