> > Why not just do:
>
> > results = list(select(...)) in those few cases where you want to have all
> > the items in any order?
>
> I guess my example was bad.
@Tom + Anand
here is my version:
added self.store[] so instead of raising IndexError, it returns
self.store[i]
# ItterBetter with reverse capabilities
class IterBetter2:
def __init__(self, iterator):
self.i, self.c = iterator, 0
self.store = []
def __iter__(self):
while 1:
x = self.i.next()
self.store.append(x)
yield x
self.c += 1
def __getitem__(self, i):
#todo: slices
if i < self.c:
return self.store[i]
try:
while i > self.c:
x = self.i.next()
self.c += 1
# if this item not in store, add it
if self.c > len(self.store):
self.store.append(x)
# now self.c == i
self.c += 1
self.store.append(self.i.next())
return self.store[i]
except StopIteration:
raise IndexError, str(i)
if __name__ == '__main__':
itr = IterBetter2(iter([x for x in range(12)]))
print itr[3], itr[2], itr[1], itr[0], itr.store
for i in itr:
print i
print itr.store
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---