views:

51

answers:

2

I'm building an ASP.NET MVC (C#) site where I want to implement STV (Single Transferable Vote) voting. I've used OpenSTV for voting scenarios before, with great success, but I've never used it programmatically.

The OpenSTV Google Code project offers a Python script that allows usage of OpenSTV from other applications:

import sys
sys.path.append("path to openstv package")

from openstv.ballots import Ballots
from openstv.ReportPlugins.TextReport import TextReport
from openstv.plugins import getMethodPlugins

(ballotFname, method, reportFname) = sys.argv[1:]

methods = getMethodPlugins("byName")
f = open(reportFname, "w")

try:
    b = Ballots()
    b.loadUnknown(ballotFname)
except Exception, msg:
    print >> f, ("Unable to read ballots from %s" % ballotFname)
    print >> f, msg
    sys.exit(-1)

try:
    e = methods[method](b)
    e.runElection()
except Exception, msg:
    print >> f, ("Unable to count votes using %s" % method)
    print >> f, msg
    sys.exit(-1)

try:
    r = TextReport(e, outputFile=f)
    r.generateReport();
except Exception, msg:
    print >> f, "Unable to write report"
    print >> f, msg
    sys.exit(-1)

f.close()

Is there a way for me to make such a Python call from my C# ASP.NET MVC site?

If so, how?

Thanks in advance!

+3  A: 

The best way is probably to use IronPython. See this answer for a starting point.

Matthew Flaschen
Great; however, how do I define a method in the Python script to call via IronPython and how do I pass in ballotFname (ballot file name) and reportFname (report file name)?
Maxim Zaslavsky
+3  A: 

Here is a good example on how to call IronPython from C#, including passing arguments and returning results; of course you'll have to make that code into a function, with ballotFname and reportFname as its arguments.

Alex Martelli