Revision d736e543
b/stv.py | ||
---|---|---|
50 | 50 |
COUNT = ".COUNT" |
51 | 51 |
RANDOM = "*RANDOM" |
52 | 52 |
THRESHOLD = "^THRESHOLD" |
53 |
|
|
54 |
logging.basicConfig(format=LOGGER_FORMAT) |
|
55 |
logging.getLogger(SVT_LOGGER).setLevel(logging.INFO) |
|
56 | 53 |
|
57 | 54 |
class Ballot: |
58 | 55 |
"""A ballot class for Single Transferable Voting. |
... | ... | |
173 | 170 |
move[1], |
174 | 171 |
times, |
175 | 172 |
move[2], |
176 |
times *move[2]) |
|
177 |
logger.info(LOG_MESSAGE.format(action=Action.TRANSFER,
|
|
178 |
desc=description)) |
|
173 |
times * move[2])
|
|
174 |
logger.debug(LOG_MESSAGE.format(action=Action.TRANSFER,
|
|
175 |
desc=description))
|
|
179 | 176 |
allocated[selected][:] = [x for x in allocated[selected] |
180 | 177 |
if x not in transferred ] |
181 | 178 |
|
... | ... | |
221 | 218 |
while num_elected < seats and num_hopefuls > 0: |
222 | 219 |
logger.info(LOG_MESSAGE.format(action=Action.COUNT_ROUND, |
223 | 220 |
desc=current_round)) |
224 |
# Log count
|
|
221 |
# Log count |
|
225 | 222 |
description = ';'.join(map(lambda x: "{0} = {1}".format(x, |
226 | 223 |
vote_count[x]), |
227 |
candidates))
|
|
224 |
hopefuls))
|
|
228 | 225 |
logger.info(LOG_MESSAGE.format(action=Action.COUNT, |
229 | 226 |
desc=description)) |
230 | 227 |
hopefuls_sorted = sorted(hopefuls, key=vote_count.get, reverse=True ) |
... | ... | |
238 | 235 |
action=Action.ELECT, |
239 | 236 |
random_generator=rnd_gen) |
240 | 237 |
hopefuls.remove(best_candidate) |
241 |
elected.append(best_candidate) |
|
238 |
elected.append((best_candidate, current_round, |
|
239 |
vote_count[best_candidate])) |
|
242 | 240 |
logger.info(LOG_MESSAGE.format(action=Action.ELECT, |
243 |
desc=best_candidate)) |
|
241 |
desc=best_candidate |
|
242 |
+ " = " + str(vote_count[best_candidate]))) |
|
244 | 243 |
if surplus > 0: |
245 | 244 |
# Calculate the weight for this round |
246 | 245 |
weight = float(surplus) / vote_count[best_candidate] |
... | ... | |
260 | 259 |
random_generator=rnd_gen) |
261 | 260 |
hopefuls.remove(worst_candidate) |
262 | 261 |
logger.info(LOG_MESSAGE.format(action=Action.ELIMINATE, |
263 |
desc=worst_candidate)) |
|
262 |
desc=worst_candidate |
|
263 |
+ " = " + str(vote_count[worst_candidate]))) |
|
264 | 264 |
redistribute_ballots(worst_candidate, hopefuls, allocated, 1.0, |
265 | 265 |
vote_count) |
266 | 266 |
|
... | ... | |
276 | 276 |
dest='ballots_file', help='input ballots file') |
277 | 277 |
parser.add_argument('--seats', nargs='?', default=0, |
278 | 278 |
dest='seats', help='number of seats') |
279 |
parser.add_argument('--loglevel', nargs='?', default=logging.INFO, |
|
280 |
dest='loglevel', help='logging level') |
|
279 | 281 |
args = parser.parse_args() |
282 |
logging.basicConfig(format=LOGGER_FORMAT) |
|
283 |
logging.getLogger(SVT_LOGGER).setLevel(args.loglevel) |
|
280 | 284 |
ballots = [] |
281 | 285 |
ballots_file = sys.stdin |
282 | 286 |
if args.ballots_file != 'sys.stdin': |
... | ... | |
292 | 296 |
(elected, vote_count) = count_stv(ballots, int(args.seats)) |
293 | 297 |
|
294 | 298 |
print "Results:" |
295 |
for candidate in elected: |
|
296 |
print candidate, vote_count[candidate] |
|
299 |
for result in elected: |
|
300 |
print result |
Also available in: Unified diff