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