def tallyFIC()

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


def tallyFIC(votes, issue):
    m = re.match(r"fic(\d+)", issue['type'])
    if not m:
        raise Exception("Not an FiC 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]
        i = 0
        for letter in vote:
            if not letter in matrix:
                matrix[letter] = 0
            matrix[letter] += numseats - i
            i += 1

    
    # Start counting
    sorted_matrix = sorted(matrix.items(), key=operator.itemgetter(1))
    bignums = heapq.nlargest(numseats, matrix.values())
    winners = [l[0] for l in sorted_matrix if matrix[l[0]] in bignums]

    # Compile list of winner names
    winnernames = []
    x = 0
    for c in winners:
        i = ord(c) - ord('a')
        winnernames.append("%s (%u points)" % ( candidates[i], bignums[x]))
        x+=1

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