我正在考虑将IronPython作为脚本语言嵌入我正在编写的应用程序中.所以为了试一试,我下载了IronPython 2.0并启动了交互式解释器.刚开始使用我的英特尔四核处理器需要大约5秒钟,内存为6 Gigs.我很难想象这对于功能较弱的机器会有多大的延迟.
基于这些基准测试,似乎IronPython的表现实际上并不是太糟糕.或者是吗?我在安装过程中错误配置了什么?或者这是交互式解释器的问题?
是否可以通过嵌入像IronPython这样的脚本语言在.NET应用程序中公开API?
怎么做?
我正在使用IronPython中的.NET API进行连接.API返回错误类型的对象(某种通用对象).我怀疑问题没有出现在他们的C#代码中,因为构造对象时的类型声明是强制返回的对象为正确的类型.是否有可能在IronPython中对一个.NET对象进行类型转换?我认为这样做会有所帮助.
我有一个带有重载静态方法的C#类,如下所示:
// Added to the Simple class in Tutorial\Extend\csextend.cs
public static int Foo(IEnumerable<int> values)
{
return 1;
}
public static int Foo(IEnumerable<string> values)
{
return 2;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从IronPython 2.6中调用它们时出错.我正在传递一个包含字符串的python列表.
import clr
clr.AddReferenceToFile("csextend.dll")
import Simple
Simple.Foo(["alpha", "bravo", "charlie"])
Run Code Online (Sandbox Code Playgroud)
TypeError: Multiple targets could match: Foo(IEnumerable[str]), Foo(IEnumerable[ int])
我的第一个问题是为什么这不起作用?似乎重载解决方案应该对此起作用.这是IronPython中的错误吗?什么是最干净的解决方法.我可以重命名例程,这样它们就不会相互重载,但后来我让铁心怪怪改变了C#类的设计.
是否有一种干净的方法可以给python一个线索,即列表完全由一种类型组成,它应该选择一个特定的重载?
与此问题相关
我正在使用一些一次性.NET对象编写一些IronPython,并想知道是否有一种很好的"pythonic"方式.目前我有一堆finally语句(我想在每个语句中都应该检查None)或者如果构造函数失败,变量是否甚至不存在?)
def Save(self):
filename = "record.txt"
data = "{0}:{1}".format(self.Level,self.Name)
isf = IsolatedStorageFile.GetUserStoreForApplication()
try:
isfs = IsolatedStorageFileStream(filename, FileMode.Create, isf)
try:
sw = StreamWriter(isfs)
try:
sw.Write(data)
finally:
sw.Dispose()
finally:
isfs.Dispose()
finally:
isf.Dispose()
Run Code Online (Sandbox Code Playgroud) 是否可以使用sqlobject从iron python连接到mysql数据库?如果是这样,怎么样?我必须安装什么?
我为cpython安装了sqlobject,它工作正常,但如果我在ironpython中使用相同的包,我会得到"ImportError:没有名为_mysql的模块".我理解这意味着ironpython无法加载访问mysql API所必需的基于C的.dll.什么是变通方法,还是有一个?
有一种方法可以在IronPython中使用NumPy/SciPy,使用IronClad来执行/与CPython二进制文件进行通信.一个较新的项目,Python Tools for VS允许更快地与.NET IronPython程序集成,因为大多数NumPy/SciPy库功能已被手动移植到IronPython中.同一页面上的评论指向此博客帖子,该博文链接到同一个github项目.
截至今天(2012年10月),从IronPython .NET python程序集成/使用这两个库的权威方法是什么?
我正在开发自定义记录器程序,根据我需要获取进程,线程和对象名称的要求在被调用函数内部(在下面的示例中,它的obj需要在get_configured_logger函数内部获取)和obj所属的类名.如下面代码中的注释所示,请提供一些实现此目的的想法.
import logging, logging.handlers
from logging import StreamHandler, Formatter
class A:
def get_configured_logger(self,name):
logger = logging.getLogger(name)
if (len(logger.handlers) == 0):
FORMAT = "%(process)s %(thread)s:-(asctime)s - %(name)s - %(levelname)s - %(message)-%(module)"
#print 'process:',process
#print 'thread:',thread
#print 'levelname:',levelname
#print 'Module:',(name portion of filename).
#print 'obj:,'name of the object(Eg:obj),current function( Eg: get_configured_logger) called by'
#print 'class name:(obj is instance of class)'
formatter = logging.Formatter(fmt=FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__=="__main__":
obj=A()
logger = obj.get_configured_logger("DEMO")
logger.debug("TEST")
Run Code Online (Sandbox Code Playgroud)
谢谢
HEMA
在我的应用程序中,我使用Iron Python来提供脚本功能.问题是嵌入式脚本没有看到我链接到应用程序的引用.根据我的理解,只有解决方案是从脚本手动导入它们
import clr
clr.AddReference(...)
from ... import ...
Run Code Online (Sandbox Code Playgroud)
但我正在从文件中读取脚本,我不想像这样添加一堆导入.那么如何从主机应用程序添加引用?ScriptEngine/ScriptScope看起来没有任何相关的方法:(
我有一个Python脚本,它从.NET应用程序接收数据.如何在我的脚本中使用'System.Collections.Generic.List`1 [System.Byte]'类型的传入缓冲区?
脚本的功能是查找和替换字符串标记,将缓冲区重新组装回System.Collections.Generic.List`1 [System.Byte],然后将缓冲区返回给.NET服务器.
我是很新的Python的.这是我到目前为止:
import array
import clr
from System.Collections.Generic import *
def SetRecvBuffer(buffer):
li = List[byte](buffer)
hookme.Debug(li)
for x in li:
hookme.Debug(x)
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.谢谢!
ironpython ×10
.net ×4
python ×4
c# ×2
python.net ×2
api ×1
mysql ×1
numpy ×1
performance ×1
python-2.7 ×1
python-3.x ×1
sqlobject ×1
using ×1
wxpython ×1