3
# Copyright 2009 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6
# Use this to calculate priorities based on Wiki priority lists such as
7
# https://launchpad.canonical.com/VersionThreeDotO/Bugs/Inputs
13
def __init__(self, scores, trailing):
15
self.trailing = trailing
18
def append_average(items, adjusted_scores, row):
19
if len(adjusted_scores) == 0:
20
avg = len(rows_of_scores)
22
avg = sum(adjusted_scores)/len(adjusted_scores)
23
items.append((avg, "||%4.1f||%s" % (avg, row.trailing)))
26
def blanks_dont_count(rows_of_scores):
28
for row in rows_of_scores:
30
for score in row.scores:
33
adjusted_scores.append(score)
34
append_average(items, adjusted_scores, row)
39
def blanks_are_heavy(rows_of_scores, half=False):
41
for row in rows_of_scores:
43
for score in row.scores:
45
score = len(rows_of_scores)
48
adjusted_scores.append(score)
49
append_average(items, adjusted_scores, row)
54
def less_is_more(rows_of_scores):
56
for row in rows_of_scores:
58
for i in range(0, len(scores)):
60
total_per_column.setdefault(i, 0)
62
total_per_column[i] += 1
63
print total_per_column
65
for row in rows_of_scores:
68
for i in range(0, len(scores)):
71
score = len(rows_of_scores)
72
weight = total_per_column[i]
73
adjusted_scores.append(score*weight)
74
append_average(items, adjusted_scores, row)
79
def condorcet(rows_of_scores):
80
raise NotImplementedError
83
def parse_scores(str):
87
ate_first_line = False
88
# We drop the first split element because the line starts with a ||
91
if not s.strip().startswith("||"):
92
# Regular output; just output it
99
if not ate_first_line:
100
ate_first_line = True
101
# Let's take a look at the header
102
if s.strip().startswith("|| * "):
103
# Get rid of scores since we're recalculating
107
head.append("|| * " + s)
111
columns = s.split("||")[delta:]
112
for col_idx, score in enumerate(columns):
113
score = score.strip()
115
first_char = score[0]
116
if not first_char.isdigit() and not first_char == "-":
117
# We hit some text, get out of here
122
# If no value was input, we assume it is equivalent to being
123
# the last option for this voter.
126
rows.append(Row(scores, '||'.join(columns)))
127
return rows, head, tail
130
if __name__ == "__main__":
131
str = sys.stdin.read().strip().splitlines()
132
rows_of_scores, head, tail = parse_scores(str)
133
if len(sys.argv) > 1 and sys.argv[1] == "less-is-more":
135
elif len(sys.argv) > 1 and sys.argv[1] == "condorcet":
137
elif len(sys.argv) > 1 and sys.argv[1] == "blanks-dont-count":
138
func = blanks_dont_count
140
func = blanks_are_heavy
141
items = func(rows_of_scores)
142
print "\n".join(head)
143
print "\n".join([i[1] for i in items])
144
print "\n".join(tail)