如何在c#中创建一个包以便在IronPython中使用

时间:2022-03-26 05:55:36

I have seen documentation on the IronPython mailing list that describes how to write an extension module in c#. I've been able to follow the documentation and it works fine. However, does anyone know how to go about producing a hierarchical extension PACKAGE in c#? For instance you are supposed to do something like this for a module:

我在IronPython邮件列表上看到了描述如何在c#中编写扩展模块的文档。我已经能够遵循文档,它工作正常。但是,有没有人知道如何在c#中生成分层扩展PACKAGE?例如,你应该为一个模块做这样的事情:

[assembly: PythonModule("my_module", typeof(test.MyModule))]
namespace test
{

    public static class MyModule
    {
        public static void hello_world()
        {
            Console.WriteLine("hello world");
        }
}

but how would you create a module called testme UNDER my_module that you would import like this:

但是你将如何创建一个名为testme UNDER my_module的模块,你可以像这样导入:

from my_module import testme

A short example or a gentle push in the right direction would be wonderful!

一个简短的例子或在正确的方向轻轻推动将是美妙的!

2 个解决方案

#1


1  

PyCrypto uses .pyd files for its native code, which are in the same folder as the Python files. I don't believe that IronPython supports that arrangement.

PyCrypto使用.pyd文件作为其本机代码,这些代码与Python文件位于同一文件夹中。我不相信IronPython支持这种安排。

Ideally you only want to implement the native parts of PyCrypto so that you can leverage the existing Python package. One option could be to create a single module (say IronPyCrypto) with the necessary classes and modify the __init__.py files in PyCrypto to say

理想情况下,您只想实现PyCrypto的本机部分,以便可以利用现有的Python包。一个选项可能是创建一个具有必要类的单个模块(比如IronPyCrypto)并修改PyCrypto中的__init__.py文件来说

# for the Hash package
if sys.platfrom == 'cli':
    from IronPyCrypto import MD2, MD4, SHA256

You lose complete compatibility with the PyCrypto source, but at least it will work.

你失去了与PyCrypto源的完全兼容性,但至少它会起作用。

#2


2  

What I do is create my C# library as I normally would e.g.

我所做的就是像往常一样创建我的C#库,例如

namespace foo.bar{

  public class Meh{
     public void CanDoSomething(){
       //does something
     }
  }

}

and then go

然后去

import clr
clr.AddReference("My.DLL") # You may want to change it to AddReferenceByPath or depending on your needs

from foo.bar import Meh

mehvar = Meh()
mehvar.CanDoSomething()

You can see some code that works for me as a test at http://www.theautomatedtester.co.uk/seleniumtraining/selenium_two_ironpython.htm

您可以在http://www.theautomatedtester.co.uk/seleniumtraining/selenium_two_ironpython.htm上看到一些适合我的代码。

#1


1  

PyCrypto uses .pyd files for its native code, which are in the same folder as the Python files. I don't believe that IronPython supports that arrangement.

PyCrypto使用.pyd文件作为其本机代码,这些代码与Python文件位于同一文件夹中。我不相信IronPython支持这种安排。

Ideally you only want to implement the native parts of PyCrypto so that you can leverage the existing Python package. One option could be to create a single module (say IronPyCrypto) with the necessary classes and modify the __init__.py files in PyCrypto to say

理想情况下,您只想实现PyCrypto的本机部分,以便可以利用现有的Python包。一个选项可能是创建一个具有必要类的单个模块(比如IronPyCrypto)并修改PyCrypto中的__init__.py文件来说

# for the Hash package
if sys.platfrom == 'cli':
    from IronPyCrypto import MD2, MD4, SHA256

You lose complete compatibility with the PyCrypto source, but at least it will work.

你失去了与PyCrypto源的完全兼容性,但至少它会起作用。

#2


2  

What I do is create my C# library as I normally would e.g.

我所做的就是像往常一样创建我的C#库,例如

namespace foo.bar{

  public class Meh{
     public void CanDoSomething(){
       //does something
     }
  }

}

and then go

然后去

import clr
clr.AddReference("My.DLL") # You may want to change it to AddReferenceByPath or depending on your needs

from foo.bar import Meh

mehvar = Meh()
mehvar.CanDoSomething()

You can see some code that works for me as a test at http://www.theautomatedtester.co.uk/seleniumtraining/selenium_two_ironpython.htm

您可以在http://www.theautomatedtester.co.uk/seleniumtraining/selenium_two_ironpython.htm上看到一些适合我的代码。