예제 #1
0
 def get_log(tally, index):
     if question_indexes is not None and index not in question_indexes:
         if 'log' not in data:
             return {}
         else:
             return data['log'][index]
     return tally.get_log()
예제 #2
0
 def get_log(tally, index):
     if question_indexes is not None and index not in question_indexes:
         if 'log' not in data:
             return {}
         else:
             return data['log'][index]
     return tally.get_log()
예제 #3
0
def stv_first_round_tiebreak(data_list):
    '''
    Tie break algorithm for stv sorting of the winners.

    When in the first round some of the winners have the same number of
    votes for the first option in ballots, it tries to solve the tie comparing
    the number of second, third, etc option in ballots for each candidate.

    NOTE: it only works to resolve ties in the first round!
    '''
    data = data_list[0]
    # get the log to get the rounds
    tallies = []
    result = agora_tally.tally.do_tally(data['extract_dir'],
                                        data['result']['counts'],
                                        tallies)

    for tally in tallies:
        log = tally.get_log()
        question = data['result']['counts'][tally.question_num]
        if "STV" not in question['a']:
            continue

        q_winners = []
        choices = __get_choices(data['extract_dir'], tally, question)
        for iteration, i in zip(log['iterations'], range(len(log['iterations']))):
            it_winners = [cand for cand in iteration['candidates']
                if cand['status'] == 'won']

            it_winners = sorted(it_winners, key=lambda winner: float(winner['count']),
                                reverse=True)

            # check if there are repeated counts
            len_set = len(set([i['count'] for i in it_winners]))
            if len_set != len(it_winners) and i == 0:
                it_winners = __stv_first_iteration_tie_break(
                    it_winners, iteration, i, data['extract_dir'], question,
                    choices, 1)
            for winner in it_winners:
                q_winners.append(winner['name'])

        question['winners'] = q_winners
예제 #4
0
def stv_first_round_tiebreak(data_list):
    '''
    Tie break algorithm for stv sorting of the winners.

    When in the first round some of the winners have the same number of
    votes for the first option in ballots, it tries to solve the tie comparing
    the number of second, third, etc option in ballots for each candidate.

    NOTE: it only works to resolve ties in the first round!
    '''
    data = data_list[0]
    # get the log to get the rounds
    tallies = []
    result = agora_tally.tally.do_tally(data['extract_dir'],
                                        data['result']['counts'],
                                        tallies)

    for tally in tallies:
        log = tally.get_log()
        question = data['result']['counts'][tally.question_num]
        if "STV" not in question['a']:
            continue

        q_winners = []
        choices = get_choices(data['extract_dir'], tally, question)
        for iteration, i in zip(log['iterations'], range(len(log['iterations']))):
            it_winners = [cand for cand in iteration['candidates']
                if cand['status'] == 'won']

            it_winners = sorted(it_winners, key=lambda winner: float(winner['count']),
                                reverse=True)

            # check if there are repeated counts
            len_set = len(set([i['count'] for i in it_winners]))
            if len_set != len(it_winners) and i == 0:
                it_winners = stv_first_iteration_tie_break(
                    it_winners, iteration, i, data['extract_dir'], question,
                    choices, 1)
            for winner in it_winners:
                q_winners.append(winner['name'])

        question['winners'] = q_winners