def tallyMNTV()

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


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

    # Start counting
    winners = [l for l in matrix if matrix[l] in heapq.nlargest(numseats, matrix.values())]

    # 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)