473,699 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An iterator with look-ahead

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(o bject):
""" 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(n ums)
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 2179
On 2007-01-10, Fredrik Lundh <fr*****@python ware.comwrote:
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_...imple-parser-1
Thank you. Much better.

--
Neil Cerutti
Jan 10 '07 #2
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
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
On 2007-01-10, Steven Bethard <st************ @gmail.comwrote :
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.
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

Neil Cerutti wrote:
On 2007-01-10, Steven Bethard <st************ @gmail.comwrote :
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.

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
You could raise a different Exception, PeekPastEndEcep tion ?
- Paddy.

Jan 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3665
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
6
4367
by: greg | last post by:
Hello all, I havent used STL containers much, as I am used to MFC containers/classes always ..The differences that I could see is iterators and algorithms. The algorithms providing some basic functinaloties which are also present in respective MFc container classes, I wanted to know what is the main powers in Iterators ?? I have written some test programs,stl containers, using iterators,and some stl algorithms, I feel the code has been...
3
2211
by: John Smith | last post by:
Hey I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build fails. Here is the troublesome code: template<class T> class CList
3
2281
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through the map, which means I must iterate through all of the sets in the map. I need an iterator that points to the set where the integer was found. How can I do this? What will the type of this iterator be? Can you help me with a code snippet?
16
2578
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
4
5701
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a vector
6
3123
by: antani | last post by:
VolumeType::ISetType::iterator findFirstIteratorMarked(const VolumeType::ISetType::iterator & it,const VolumeType::ISetType::iterator & e_it) { VolumeType::ISetType::iterator _it=it; while (_it!=e_it) { if ((*_it).mark) return _it; else _it++; }
27
5310
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
1
2774
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed objects. My goal here is to abstract away the database part, so that in the future these objects could be constructed from a data source on disk, across the network, etc. The interface I'm after is similar to the standard iterator interface for an...
5
2285
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator from a function and b = iter() # these two are just examples.
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8945
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7787
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4392
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4641
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.