Peter Otten
2/13/2008 9:32:00 AM
James Stroud wrote:
groupby() is "all you can eat", but "no doggy bag".
> def serialize(table, keyer=_keyer,
> Â Â Â Â Â Â Â Â Â Â Â selector=_selector,
> Â Â Â Â Â Â Â Â Â Â Â keyfunc=_keyfunc,
> Â Â Â Â Â Â Â Â Â Â Â series_keyfunc=_series_keyfunc):
> Â Â keyed = izip(imap(keyer, table), table)
> Â Â filtered = ifilter(selector, keyed)
> Â Â serialized = groupby(filtered, series_keyfunc)
> Â Â serieses = []
> Â Â for s_name, series in serialized:
> Â Â Â grouped = groupby(series, keyfunc)
> Â Â Â regrouped = ((k, (v[1] for v in g)) for (k,g) in grouped)
> Â Â Â serieses.append((s_name, regrouped))
You are trying to store a group for later consumption here.
> Â Â for s in serieses:
> Â Â Â yield s
That doesn't work:
>>> groups = [g for k, g in groupby(range(10), lambda x: x//3)]
>>> for g in groups:
.... print list(g)
....
[]
[]
[]
[9]
You cannot work around that because what invalidates a group is the call of
groups.next():
>>> groups = groupby(range(10), lambda x: x//3)
>>> g = groups.next()[1]
>>> g.next()
0
>>> groups.next()
(1, <itertools._grouper object at 0x2b3bd1f300f0>)
>>> g.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Perhaps Python should throw an out-of-band exception for an invalid group
instead of yielding bogus data.
Peter