def tallyFPP()

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


def tallyFPP(votes, issue):
    candidates = []
    for c in issue['candidates']:
        candidates.append(c['name'])
    

    debug = []
    matrix = {}
    
    # Set up counting matrix
    for key in votes:
        vote = votes[key]
        matrix[vote] = (matrix[vote] if vote in matrix else 0) + 1
    
    l = []
    for x in matrix:
        l.append(matrix[x])
        
    cc = []
    for x in matrix:
        if matrix[x] == max(l):
            cc.append(x)
    winners = []
    winnernames = []
    
    for c in cc:
        i = ord(c) - ord('a')
        winners.append(c)
        winnernames.append(candidates[i])


    # Return the data
    js = {
        'votes': len(votes),
        'winners': winners,
        'winnernames': winnernames,
        'winnerpct': ((1.00*max(l)/len(votes))*100) if len(votes) > 0 else 0.00,
        'tie': True if len(winners) > 1 else False
    }
    
    return js, """
Winners:
 - %s
""" % "\n - ".join(["%s (%f%%)" % (n,js['winnerpct']) for n in winnernames])