[PEAK] Progress on peak.schema
Phillip J. Eby
pje at telecommunity.com
Tue Mar 1 10:45:45 EST 2005
At 11:03 AM 3/1/05 +0200, Niki Spahiev wrote:
>Phillip J. Eby wrote:
>[..]
>
>>Anyway, here's a couple of ways to express the same simple query using
>>the "bulletins" example schema::
>> aUser = ws.groupBy(User.loginId)['joe']
>> aUser = ws[User.loginId.eq('joe')]
>
>Has ws.groupBy any connection to SQL group by? If not it's confusing.
Some. Actually, I just realized that the above is wrong, it should be:
aUser, = ws.groupBy(User.loginId)['joe']
Because groupBy returns a mapping whose values are sets. That is, it's a
mapping keyed by the specified keys, with values being the sets of objects
matching the corresponding keys. So this prints all product categories,
each with its average price::
for category, products in ws[Product].groupBy(Product.category).items():
print category.name, sum(p.price for p in products)/len(products)
Although in the long run there will be a way to do such a report directly
in the query language.
Anyway, do you have an alternate suggestion for the operation's name?
More information about the PEAK
mailing list