print() print("== TALLY COMPLETE") print() print("The winners are, in order of election:") print() for candidate in provisionallyElected: print(" {}".format(candidate.name)) print() print("---- Exhausted: {}".format(toNum(exhausted))) if args.countback: candidate = next(x for x in candidates if x.name == args.countback[0]) print("== STORING COUNTBACK DATA FOR {}".format(candidate.name)) # Sanity check ctvv = 0 for ballot in candidate.ballots: ctvv += ballot.value assert ctvv == candidate.ctvv candidatesToExclude = [] for peCandidate in provisionallyElected: candidatesToExclude.append(peCandidate) with open(args.countback[1], 'w') as countbackFile: # use --noround to determine whether to use standard BLT format or rational BLT format stringify = str if args.noround else float utils.writeBLT(candidate.ballots, candidates, 1, candidatesToExclude, countbackFile, stringify)
import argparse, itertools, json, sys parser = argparse.ArgumentParser(description='Convert a Helios election result to an OpenSTV blt file.') parser.add_argument('election', help='Helios-style election.json specifying candidates') parser.add_argument('result', help='Helios-style gamma encoded result.json specifying ballots') parser.add_argument('seats', type=int, help='The number of candidates to elect') parser.add_argument('question', type=int, help='The question number to tally', nargs='?', default=0) args = parser.parse_args() candidates = [] with open(args.election, 'r') as electionFile: election = json.load(electionFile) candidates = [] for candidate in election["questions"][args.question]["answers"]: candidates.append(utils.Candidate(candidate.split("/")[0])) # Just want the name with open(args.result, 'r') as resultFile: results = json.load(resultFile) result_squashed = [x[0] if isinstance(x, list) else x for x in results[args.question]] ballots = [] # Preprocess groups for result, group in itertools.groupby(sorted(result_squashed)): preferences = utils.to_absolute_answers(utils.gamma_decode(result, len(candidates)), len(candidates)) ballots.append(utils.Ballot([candidates[x] for x in preferences], None, len(list(group)))) utils.writeBLT(ballots, candidates, args.seats)