Re: [Tutor] __iter__: one obvious way to do it

2010-03-07 Thread Hugo Arts
On Sun, Mar 7, 2010 at 5:37 PM, Steven D'Aprano wrote: >> "There should be one-- and preferably only one --obvious way to do >> it" http://www.python.org/dev/peps/pep-0020/ > > This doesn't mean that there should be *only* one way to do something. > It means that the should be one OBVIOUS way to d

Re: [Tutor] __iter__: one obvious way to do it

2010-03-07 Thread Steven D'Aprano
On Mon, 8 Mar 2010 02:07:41 am spir wrote: > [sorry, forgot the code] > > Hello, > > Below 6 working way to implement __iter__ for a container here > simulated with a plain inner list. Sure, the example is a bit > artificial ;-) > 1. __iter__ returns a generator _expression_ > def __iter__(sel

[Tutor] __iter__: one obvious way to do it

2010-03-07 Thread spir
[sorry, forgot the code] Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a

[Tutor] __iter__: one obvious way to do it

2010-03-07 Thread spir
Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-28 Thread Lie Ryan
> > > 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 >

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-28 Thread Eric Abrahamsen
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

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-26 Thread Kent Johnson
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

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-26 Thread Alan Gauld
"Eric Abrahamsen" <[EMAIL PROTECTED]> wrote 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 sourc

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-26 Thread Eric Abrahamsen
On Aug 26, 2008, at 7:20 PM, Kent Johnson wrote: On Tue, Aug 26, 2008 at 1:36 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote: So my test case: a Month has a 'child' attribute pointing at Week, which has a 'child' attribute pointing at Day, so they all know what kind of child instances itera

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-26 Thread Kent Johnson
On Tue, Aug 26, 2008 at 1:36 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote: > So my test case: a Month has a 'child' attribute pointing at Week, which has > a 'child' attribute pointing at Day, so they all know what kind of child > instances iteration should produce. With nested loops, a Month pro

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Eric Abrahamsen
I do apologize for the large quantities of confusing description – articulating the problem here has helped me understand exactly what it is I'm after (though it hasn't improved my code!), and I've got a better grasp of the problem now than I did when I first asked. It isn't so much that I

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Kent Johnson
I'm not following your code very well. I don't understand the relationship between the first loop and the iter_children() function. A couple of things that might help: - Django QuerySets can be qualified with additional tests, so you could have each of your month/week/etc classes have its own corr

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Eric Abrahamsen
Okay I think I'm onto something, more iterator-related stuff. If I can make self.events an iterator, then run a for loop on it, breaking out of the loop when the events' date attributes get too high. Then on the next run through, that same for loop should pick up where it left off, right? H

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-24 Thread Eric Abrahamsen
On Aug 24, 2008, at 7:20 PM, Kent Johnson wrote: Forwarding to the list with my reply. Please use Reply All to reply to the list. Grr, sorry, I keep forgetting... On Sun, Aug 24, 2008 at 1:02 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote: On Aug 23, 2008, at 11:22 PM, Kent Johnson wrot

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-23 Thread Kent Johnson
On Sat, Aug 23, 2008 at 6:47 AM, Eric Abrahamsen <[EMAIL PROTECTED]> wrote: > At first I thought the bisect module was the way to go, but it is too > tightly tied to integer list indices, and works very awkwardly when > bisecting on datetime attributes. I'm not sure what the problem is with bisect

[Tutor] __iter__ loops, partitioning list among children

2008-08-23 Thread Eric Abrahamsen
Hi, I've got a problem that takes a bit of explaining, but it's relatively simple when you get down to it. This is another django-related thing, but the issue itself is pure python. I made a custom class, called an EventEngine, which represents a span of time. You initialize it with a que

Re: [Tutor] __iter__

2006-01-17 Thread Danny Yoo
> Where/how/when is 'def next(self(:' called? The 'for' loop statement does this internally when it marches across an iterable thing. We can write something something like this: ## for item in thing: print item ## Python is doing something like this

Re: [Tutor] __iter__

2006-01-17 Thread Danny Yoo
> One small correction: Pointer should have an __iter__() method that > returns self; this is part of the iterator protocol. See PEP 234 > http://www.python.org/peps/pep-0234.html Hi Kent, Ah, thank you! Ok, the corrected code is: # class MyListOf

Re: [Tutor] __iter__

2006-01-17 Thread Kent Johnson
Danny Yoo wrote: > > On Mon, 16 Jan 2006, Christopher Spears wrote: > > >>I'm not sure if I understand __iter__. You use it to create an object >>that iterates through itself using a next menthod ? > > > Hi Chris, > > Yes, that's one application. > > > But __iter__() doesn't necessarily ha

Re: [Tutor] __iter__

2006-01-17 Thread Rinzwind
danny: my apologies for sending this to your email instead of the list!! (There I was looking at the list going: WTF DOES IT TAKE SO LONG when it hit me...). *cuts in* No I don't :-) With this: class MyListOfNumbers: def __init__(self, data): self.data = data def __iter__(self):

Re: [Tutor] __iter__

2006-01-16 Thread Danny Yoo
On Mon, 16 Jan 2006, Christopher Spears wrote: > I'm not sure if I understand __iter__. You use it to create an object > that iterates through itself using a next menthod ? Hi Chris, Yes, that's one application. But __iter__() doesn't necessarily have to return 'self'. For example, here's

Re: [Tutor] __iter__

2006-01-16 Thread Kent Johnson
Christopher Spears wrote: > I'm not sure if I understand __iter__. You use it to > create an object that iterates through itself using a > next menthod ? Yes, you seem to have it right. Your code is fine IMO. In many cases it's easier to use a generator than an iterator class. If you are using

[Tutor] __iter__

2006-01-16 Thread Christopher Spears
I'm not sure if I understand __iter__. You use it to create an object that iterates through itself using a next menthod ? class Squares: def __init__(self, start, stop): self.value = start - 1 self.stop = stop def __iter__(self): ret