def tallyDH()

in pysteve/lib/plugins/dh.py [0:0]


def tallyDH(votes, issue):
    m = re.match(r"dh(\d+)", issue['type'])
    if not m:
        raise Exception("Not a D'Hondt vote!")
    
    numseats = int(m.group(1))
    candidates = []
    for c in issue['candidates']:
        candidates.append(c['name'])
    

    debug = []
    
    # Set up letters for mangling
    letters = [chr(i) for i in range(ord('a'), ord('a') + len(candidates))]
    cc = "".join(letters)
    
    # Set up seats won
    winners = []
   
    # Set up vote matrix 
    matrix = {}
    for key in votes:
        vote = votes[key]
        if not vote in matrix:
            matrix[vote] = [0,1]
        matrix[vote][0] += 1

    # Start counting
    while len(winners) < numseats:
        m = []
        for c in matrix:
            quotient = (matrix[c][0]/matrix[c][1])
            m.append(quotient)
        for c in matrix:
            quotient = (matrix[c][0]/matrix[c][1])
            if quotient == max(m):
                winners.append(c)
                matrix[c][1] += 1
                break

    # Compile list of winner names
    winnernames = []
    for c in winners:
        i = ord(c) - ord('a')
        winnernames.append(candidates[i])

    # Return the data
    return {
        'votes': len(votes),
        'winners': winners,
        'winnernames': winnernames,
    }, """
Winners:
 - %s
""" % "\n - ".join(winnernames)