vote_tally.ballot_generator.ballot_generator

Create a random, valid group of candidates and ballots in to a csv

 1#! usr/bin/env python
 2"""
 3Create a random, valid group of candidates and ballots
 4in to a csv
 5"""
 6import random
 7import argparse
 8import pandas as pd
 9
10def generate_ballots(n_candidates,n_voters,output):
11    """
12    Creates a csv of randomised candidates and votes
13
14    Parameters
15    ----------
16    n_candidates : int
17        Number of candidates to generate (alphabetically)
18    n_voters : int
19        Number of voter ballots to generate
20    output : string
21        Output location of the csv
22    """
23    candidates = []
24    for cand_no in range(n_candidates):
25        tmp_name = 'abcdefghijklmnopqrstuvwxyz'[cand_no]
26        for _ in range(random.randint(3,10)):
27            if random.random() > 0.7:
28                tmp_name += random.choice('aeiouy')
29            else:
30                tmp_name += random.choice('bcdfghjklmnpqrstvwxz')
31        candidates.append(tmp_name.title())
32    votes = []
33    for _ in range(n_voters):
34        tmp_vote = list(range(1,n_candidates+1))
35        random.shuffle(tmp_vote)
36        votes.append(tmp_vote)
37
38    ballots = pd.DataFrame(votes,columns=candidates)
39    ballots.to_csv(output,index=False)
40
41def main():
42    """
43    # Reports the winner of the election from test data
44    """
45    parser = argparse.ArgumentParser(
46            description='Create random votes')
47    parser.add_argument('-o','--output',
48        type=str,
49        default='data/random_votes_1.csv',
50        help='Output location of votes csv file')
51    parser.add_argument('--cand',
52        type=int,
53        default=10,
54        help='Number of random candidates')
55    parser.add_argument('--voters',
56        type=int,
57        default=20,
58        help='Number of voters')
59    args = parser.parse_args()
60
61    generate_ballots(args.cand,args.voters,args.output)
62
63if __name__ == "__main__":
64    main()
def generate_ballots(n_candidates, n_voters, output):
11def generate_ballots(n_candidates,n_voters,output):
12    """
13    Creates a csv of randomised candidates and votes
14
15    Parameters
16    ----------
17    n_candidates : int
18        Number of candidates to generate (alphabetically)
19    n_voters : int
20        Number of voter ballots to generate
21    output : string
22        Output location of the csv
23    """
24    candidates = []
25    for cand_no in range(n_candidates):
26        tmp_name = 'abcdefghijklmnopqrstuvwxyz'[cand_no]
27        for _ in range(random.randint(3,10)):
28            if random.random() > 0.7:
29                tmp_name += random.choice('aeiouy')
30            else:
31                tmp_name += random.choice('bcdfghjklmnpqrstvwxz')
32        candidates.append(tmp_name.title())
33    votes = []
34    for _ in range(n_voters):
35        tmp_vote = list(range(1,n_candidates+1))
36        random.shuffle(tmp_vote)
37        votes.append(tmp_vote)
38
39    ballots = pd.DataFrame(votes,columns=candidates)
40    ballots.to_csv(output,index=False)

Creates a csv of randomised candidates and votes

Parameters
  • n_candidates (int): Number of candidates to generate (alphabetically)
  • n_voters (int): Number of voter ballots to generate
  • output (string): Output location of the csv
def main():
42def main():
43    """
44    # Reports the winner of the election from test data
45    """
46    parser = argparse.ArgumentParser(
47            description='Create random votes')
48    parser.add_argument('-o','--output',
49        type=str,
50        default='data/random_votes_1.csv',
51        help='Output location of votes csv file')
52    parser.add_argument('--cand',
53        type=int,
54        default=10,
55        help='Number of random candidates')
56    parser.add_argument('--voters',
57        type=int,
58        default=20,
59        help='Number of voters')
60    args = parser.parse_args()
61
62    generate_ballots(args.cand,args.voters,args.output)

Reports the winner of the election from test data