Skip to content

Instantly share code, notes, and snippets.

@reubano
Last active June 20, 2017 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reubano/f79023952f71604ca22487342a4bba7a to your computer and use it in GitHub Desktop.
Save reubano/f79023952f71604ca22487342a4bba7a to your computer and use it in GitHub Desktop.
Voting script and sample ballot file used for [Open Data Day Arusha 2017](https://www.reubano.xyz/blog/arusha-s-first-open-data-day/)
#!/usr/bin/env python2
#
import csv, sys
from io import open
from collections import defaultdict
from pyvotecore.schulze_stv import SchulzeSTV
notation = SchulzeSTV.BALLOT_NOTATION_RANKING
_ballots = defaultdict(int)
args = sys.argv[1:]
path = args[0]
num = int(args[1] if len(args) > 1 else 3)
with open(path) as f:
for ballot in csv.DictReader(f):
key = tuple(sorted(tuple([k, v]) for k, v in ballot.items() if v))
_ballots[key] += 1
ballots = [{'count': v, 'ballot': dict(k)} for k, v in _ballots.items()]
result = SchulzeSTV(ballots, required_winners=num, ballot_notation=notation)
for winner in result.winners:
print winner
EVANS GREY THOMSON AMES REID SPEARS WHITE
1 2
1 2 3
3 1 2 4
1 4 3 2
1
1
1
1
1
1
1 2
2 1
1
1
1 2
1
1
1
1 2 3
1 2 3
1
1 2
1
1
1 2
2 1
1
3 1 4 2
1
1
1
1 3 2 4
1
1 2
1 2
3 1 2 4
1
1 2 3
1 2
1 3 2 4
1 2
1 2 3
1
1 2 3
3 1 2
1 2 3
1
1 2 3
1 2
1
1 4 3 2
3 1 2 4
1
1
2 1
1 2 3
4 1 3 2 5
1 2
1 3 2 4
1
1 2
1 2 3
4 1 5 2 3
1
1
1 7 4 5 1 6 3

install the requirements (requires Python 2)

pip install python-vote-full

make the script executable

chmod 0755 count_votes

Tally the ballots and show (by default) the top 3 choices

./count_votes example_ballot.csv
SPEARS
GREY
AMES

Tally the ballots and show the top 2 choices

./count_votes example_ballot.csv 2
GREY
AMES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment