I finally got my iterator-based version working, only to discover that it's nearly four times slower than the brute-force multiple-loops version I started with! Then I tried just adding an incrementing index to the loop, so that each loop only ran through self.events[last_index:], but that was still twice as slow as without the index. I suppose it's the overhead of incrementing the variable, or maybe some optimization in Python's internals, but the take home lesson was definitely 'leave well enough alone'. Anyway, thanks again for the advice, it's been a learning experience...

E


On Aug 27, 2008, at 2:22 AM, Kent Johnson wrote:

On Tue, Aug 26, 2008 at 1:24 PM, Eric Abrahamsen
<[EMAIL PROTECTED]> wrote:
On Aug 26, 2008, at 7:20 PM, Kent Johnson wrote:

If all you want to do with the nested Month, etc is to iterate the
events in them, you could probably use a shared iterator. It would
have to be able to push-back items so that when you hit the
out-of-range item you could push it back on the iterator.

Is a 'shared iterator' something special, or do you mean all the instances would draw their events from a single iterator? I'm not sure what this would
look like.

It's nothing special, I just mean that all instances would share an
iterator. You would pass the iterator to the iteration function.

Just for the sake of argument, here's the principle I'm working from:

#####
lst = range(10)
iterlst = iter(lst)
iterlst.next()
0
for x in iterlst:
...   if x < 5:
...     print x
...   else:
...     break
...
1
2
3
4
for x in iterlst:
...   print x
...
6
7
8
9
#####

So that's why I'm creating the iterator outside of the while loop in the original code, and then using a repeated for loop with a break to step through all the events only once. Of course, the fact that 5 isn't in there
probably points to the source of my problems! The solution might be
assigning iterlist.next() to a variable, and advancing the variable.

The problem is that the first loop consumes the 5 from the iterator
but doesn't actually process it. That is why you need an iterator with
push-back - so you can put the 5 "back in" the iterator and get it out
again in the next loop.
http://code.activestate.com/recipes/502304/
Though working with indices directly might be simpler.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to