Given a list of iterators, I'd like to have a new one that would
cyclically walk over the list calling the next() method of the iterators
(removing any iterator which is exhausted). It should also support adding
a new iterator to the list; it should be added in front of the current
position (so that it's called only after all the others). This is what I
was able to write with my zero python skills (self.iters is the list of
iterators, self.i is the index of the iterator that should be used next).
Is there a nicer/more pythonic solution, maybe using generators?
class Liter(object):
def __init__(self, *iters):
self.i=0
self.iters=[iter(x) for x in iters]
def append(self,what):
self.iters.insert(self.i,what)
def __iter__(self):
return self
def next(self):
while True:
try:
result=self.iters[self.i].next()
except StopIteration:
del self.iters[self.i]
except IndexError:
if len(self.iters) is 0:
raise StopIteration
else:
self.i=0
else:
self.i+=1
return result 3 1617
Hmm, here's an approach using the .throw() operation from PEP 342.
It's obviously untested, since that feature is not currently part of
Python, probably incorrect, and maybe just insane. I renamed "append"
to "insert_iterator" since "append" usually means put something at the
end, not in the middle.
from itertools import cycle
class InsertIterator(Exception): pass
def itergen(self, *iters):
while iters:
try:
for i,it in (enumerate(it) for it in cycle(iters)):
yield it.next()
except StopIteration:
del iters[i]
except InsertIterator, new_iterator:
# maybe the i here should be i+1?
iters = [new_iterator] + iters[i:] + iters[:i]
Now you can say
ig = itergen(it1,it2,...)
for x in ig:
....
and if you want to insert a new iterator, just say
ig.throw(InsertIterator, new_iterator)
Paul Rubin <http://ph****@NOSPAM.invalid> writes: Hmm, here's an approach using the .throw() operation from PEP 342. It's obviously untested, since that feature is not currently part of Python, probably incorrect, and maybe just insane.
Pardon the unintentional "shift/reduce conflict" above. I mean that
the code sample I posted is probably incorrect and maybe insane, not
that the proposed throw() feature is incorrect or insane. se****@sophia.dtp.fmph.uniba.sk wrote: Given a list of iterators, I'd like to have a new one that would cyclically walk over the list calling the next() method of the iterators (removing any iterator which is exhausted). It should also support adding a new iterator to the list; it should be added in front of the current position (so that it's called only after all the others). This is what I was able to write with my zero python skills (self.iters is the list of iterators, self.i is the index of the iterator that should be used next). Is there a nicer/more pythonic solution, maybe using generators?
A generator version:
def iterconcat (collectionlist):
for collection in collectionlist:
for element in collection:
yield element
Extending collectionlist at runtime will work only with lists and
similar collections. And you'll need to keep a reference to it as well.
And it would be called after all the others, which matches your
description, but not your code.
Daniel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: bdinmstig |
last post by:
I refined my attempt a little further, and the following code does
seem to work, however it has 2 major problems:
1. Very limited support for XPath features
Basic paths are supported for...
|
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...
|
by: John Flynn |
last post by:
hi,
having problems reading from and writing back to the same file.
basically, i want to read lines of text from a file and reverse them and
write them back to the same file.. it has to...
|
by: nick |
last post by:
Hi,
I need to manage a "layered" collection of objects, where each layer
grows independently, e.g,
o--+--+--+--+--+ 1st layer
|
o 2nd layer (empty)
|
o--+--+--+ 3rd...
|
by: apex |
last post by:
Hi all:
I want to write an iterator class for GHashTable which belongs to GNU
glib .Access function of GHashTable now only support internal
iterator and define
struct _GHashNode
{
gpointer ...
|
by: Pierre Thibault |
last post by:
Hello!
I am currently trying to port a C++ code to python, and I think I am stuck
because of the very different behavior of STL iterators vs python
iterators. What I need to do is a simple...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
|
by: mkborregaard |
last post by:
Hi,
I have the weirdest problem, and I can not see what is going wrong.
I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
|
by: remlostime |
last post by:
struct nodeType
{
int v, index;
};
list<nodeTypenode;
for(list<nodeType>::iter = node].begin();
iter != node].end(); iter++)
What's wrong with it? how can i fix it?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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,...
| |