从C#调用Python应用程序/脚本 [英] Calling Python app/script from C#

查看:670
本文介绍了从C#调用Python应用程序/脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,我想实现的 STV(转移单票的)投票。我用OpenSTV表决前的情景,获得巨大成功,但我从来没有使用过编程。

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.

借助 OpenSTV调用脚本谷歌code项目提供了一个Python脚本,允许OpenSTV的使用从其他应用程序:

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

有我从我的C#ASP.NET MVC网站做​​出这样一个Python电话?一种方法

如果是这样,怎么样?

在此先感谢!

推荐答案

这里是关于如何从C#中,包括传递参数和返回结果调用IronPython的一个很好的例子;当然,你必须作出这样的code到一个函数,用 ballotFname reportFname 作为参数

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.

这篇关于从C#调用Python应用程序/脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆