1

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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.