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 5 2184
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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
|
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?
|
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?
}
| |
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
|
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++;
}
|
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()
|
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...
|
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.
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
| |
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...
|
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...
| |