-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroupquery.py
42 lines (33 loc) · 1.35 KB
/
groupquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# groupquery.py
# Allow group queries.
# Not integrated with the command line, it would probably be too cumbersome to
# use it there; use it in scripts instead.
def groupquery(cards, groupf, accf, acc_default):
""" Do a query not unlike SQL's GROUP BY.
groupf(card) is a function that determines the group.
accf(card, value) takes a value, computes a new value from the given
card, and "accumulates" them, returning a new value.
acc_default is the initial accumulator value.
"""
groups = {}
for card in cards:
group = groupf(card)
group_val = groups.get(group, acc_default)
groups[group] = accf(card, group_val)
return groups
if __name__ == "__main__":
import sys
short_sets = sys.argv[1:]
import cardloader
loader = cardloader.CardLoader(short_sets)
cards = loader.load_cards()
#
# example: compute the percentage of artifacts in each set
def group_by_set(card):
return card['set'].shortname
def acc_artifacts(card, (artifacts, total)):
return (artifacts + int(card.type('artifact')), total+1)
results = groupquery(cards, group_by_set, acc_artifacts, (0, 0))
for set, (artifacts, total) in sorted(results.items()):
print "%s: %d cards, %d artifacts\t=> %4.1f%%" % (set, total,
artifacts, 100.0 * artifacts / total)