如何在IronPython中创建.Net Web服务?优选地,动态地没有任何预处理或后处理.
我在C#中嵌入了IronPython 2.0.在IronPython中,我用以下内容定义了自己的异常:
def foobarException(Exception):
pass
Run Code Online (Sandbox Code Playgroud)
并将其提升到某个地方:
raise foobarException( "This is the Exception Message" )
Run Code Online (Sandbox Code Playgroud)
现在在C#中,我有:
try
{
callIronPython();
}
catch (Exception e)
{
// How can I determine the name (foobarException) of the Exception
// that is thrown from IronPython?
// With e.Message, I get "This is the Exception Message"
}
Run Code Online (Sandbox Code Playgroud) 除了CPython,其他Python实现目前可用于生产系统?
问题
已经阐明了有关该主题的利弊.我现在想知道,如果那些更奇特的实现实际上用于必须可靠运行的系统中.(可能的例子?开源?)
编辑:我要求的代码需要Python版本> = 2.5
假设我有以下IronPython脚本:
def Calculate(input):
return input * 1.21
Run Code Online (Sandbox Code Playgroud)
当使用小数从C#调用时,此函数返回一个double:
var python = Python.CreateRuntime();
dynamic engine = python.UseFile("mypythonscript.py")
decimal input = 100m; // input is of type Decimal
// next line throws RuntimeBinderException:
// "cannot implicitly convert double to decimal"
decimal result = engine.Calculate(input);
Run Code Online (Sandbox Code Playgroud)
我似乎有两个选择:
首先,我可以在C#侧投射:看起来像是一个禁区,因为我可能会失去精确度.
decimal result = (decimal)engine.Calculate(input);
Run Code Online (Sandbox Code Playgroud)
第二个选项是在python脚本中使用System.Decimal:工作,但稍微污染脚本...
from System import *
def CalculateVAT(amount):
return amount * Decimal(1.21)
Run Code Online (Sandbox Code Playgroud)
是否有一个简写符号DLR,数字1.21应该被解释为十进制,就像我在C#中使用'1.21m'符号一样?或者有没有其他方法来强制使用十进制而不是double?
我需要迭代每一行,将项添加到字典中,进行一些排序,然后将结果吐出到我需要通过脚本动态创建的数据表中.
我再也无法访问这篇文章了,有没有人有代码可以做类似的事情呢?
谢谢
我正在尝试执行一个Python脚本,该脚本使用Python For .Net(https://github.com/pythonnet/pythonnet)来加载名为"Kratos_3.dll"的C#库,该库与脚本位于同一文件夹中但文件无法找到.
我使用"pip install pythonnet"安装了clr.
这是我的脚本:
import clr
import sys
sys.path.insert(0,"C:\\dev\\proj_1\\")
clr.AddReference("Kratos_3")
Run Code Online (Sandbox Code Playgroud)
我一直在收到错误
FileNotFoundException: Unable to find assembly 'Kratos_3. at Python.Runtime.CLRModule.AddReference(String name)
Run Code Online (Sandbox Code Playgroud)
当我使用IronPython运行它时,它可以工作,但我想使用常规Python 2.7来实现这一点,我需要做什么?
我被告知,由C#和F#源组成的汇编文件可以互操作,因为它们被编译成.NET程序集.
任何示例代码都会很棒.
为什么你可以在不需要GIL但是Python(CPython)需要GIL的情况下运行Jython和IronPython?
我写了一个小脚本用于工作,当开发它时一切都很好但是当我把它翻过来用于生产时,人们抱怨显示第一个表单所花费的时间.展望它,似乎总是花在导入脚本使用的各种模块上.我创建了一个只包含import语句和打印Hello World的新脚本,然后将它作为python脚本和可执行文件运行,结果如下.发生了什么事,有什么方法可以加快速度吗?
Python文件:
$ for i in {1..10}
> do
> time ./ipy.exe time.py
> done
real 0m1.712s
real 0m1.701s
real 0m1.689s
real 0m1.691s
real 0m1.709s
real 0m1.663s
real 0m1.697s
real 0m1.700s
real 0m1.699s
real 0m1.741s
Run Code Online (Sandbox Code Playgroud)
使用ipy pyc.py /main:time.py/target:exe构建的exe
$ for i in {1..10}
> do
> time ./time.exe | grep -v user | grep -v sys
> done
real 0m22.119s
real 0m22.116s
real 0m22.133s
real 0m21.816s
real 0m21.985s
real 0m21.785s
real 0m22.010s
real 0m21.686s
real 0m21.877s
real 0m21.944s
Run Code Online (Sandbox Code Playgroud)
time.py的内容: …
我在VS2012中使用IronPython并尝试调用.Net函数,该函数接受Ref参数,
Lib.dll
public int GetValue(ref double value)
{
...
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
import clr
clr.AddReference('Lib.dll')
from LibDll import *
value =0.0
x = GetValue(value)
Run Code Online (Sandbox Code Playgroud)
我错过了什么,在C#中我们ref一起使用变量名,在Python中这是什么?
ironpython ×10
.net ×4
python ×3
c# ×2
jython ×2
.net-4.0 ×1
cpython ×1
exception ×1
f# ×1
gil ×1
ironruby ×1
literals ×1
pypy ×1
python-2.7 ×1
python.net ×1
spotfire ×1
types ×1
web-services ×1