| Store | Cart

Suggesting a new feature - "Inverse Generators"

From: Michael Spencer <m...@telcopartners.com>
Fri, 25 Mar 2005 10:41:49 -0800
Jordan Rastrick wrote:
> Wow, if I'm going to get replies (with implemented solutions!) this> quickly, I'll post here more often :-)

That is indeed typical of this most attentive group :-)
> > Its taken me a while to get a rough understanding of this code, but I> think I have some idea. 
It is just an example jotted in 2 min - no doubt it could be made clearer.

> Correct me if I'm wrong.> groupby groups based on value of line(record)> 
No, groupby, groups on the value of record(item), where item is given by 
iterating over linesource

You should check the itertools documentation:
http://docs.python.org/lib/itertools-functions.html

> Record returns 1 for the first line, 1 of the second, 1 for the 3rd,> then 2 for the 4th because seq[0] gets incremented since len(line) > 20

In this case, it doesn't matter what record returns, as long as it is equal for 
successive values of item that should be grouped
> > OK thats fair enough. But how does record retain state between calls?> How is that related to the fact your storing your value as a singleton> list, instead just an int?

You are asking about the fundamental behavior of Python: argument passing, 
mutable objects and scopes.
> > It seems a little confusing to be honest, probably mainly due to my> unfamiliarity with groupby. Retaining state between method calls is> part of what interests me so much about the Generator/ Acceptor case.> Here youre retaining state between calls with none of the special> syntax used for example in Generators.> > How? Is it a side effect of the way groupby uses record? If so, then> thats a littleoblique and unreadable for my liking.

No, it's nothing special about groupby.  record simply stores its state in a 
mutable default parameter.  This isn't general good practice: at least you have 
to be careful with it.  You can see the behavior in the following example:
  >>> def accumulate(value, accum = []):
  ...     accum.append(value)
  ...     return accum
  ...
  >>> accumulate(1)
  [1]
  >>> accumulate(2)
  [1, 2]
  >>> accumulate(6)
  [1, 2, 6]
  >>>

...
> > Still, this is fascinating.... going to have to spend some time> experimenting with groupby as soon as I get a chance....> 
Experimenting is good.  So is the the documentation: 
http://docs.python.org/tut/tut.html

Michael

Recent Messages in this Thread
Jordan Rastrick Mar 25, 2005 04:04 pm
Jordan Rastrick Mar 25, 2005 04:10 pm
Andrew Koenig Mar 25, 2005 04:18 pm
Jordan Rastrick Mar 25, 2005 04:36 pm
Andrew Koenig Mar 26, 2005 01:40 pm
Tim Hochberg Mar 25, 2005 04:25 pm
Michael Spencer Mar 25, 2005 04:46 pm
Jordan Rastrick Mar 25, 2005 05:23 pm
Michael Spencer Mar 25, 2005 06:41 pm
Serge Orlov Mar 25, 2005 07:14 pm
Jordan Rastrick Mar 26, 2005 06:56 am
Peter Otten Mar 26, 2005 08:04 am
Scott David Daniels Mar 25, 2005 07:34 pm
Michael Spencer Mar 25, 2005 08:07 pm
Scott David Daniels Mar 25, 2005 09:00 pm
Bengt Richter Mar 25, 2005 10:11 pm
Bengt Richter Mar 25, 2005 07:43 pm
Terry Reedy Mar 25, 2005 07:13 pm
Jordan Rastrick Mar 25, 2005 04:49 pm
Diez B. Roggisch Mar 25, 2005 06:13 pm
Jordan Rastrick Mar 26, 2005 04:11 pm
phil...@yahoo.com Mar 27, 2005 03:44 am
Oren Tirosh Mar 27, 2005 06:58 am
Messages in this thread