开发者

How to create a package in c# for use in IronPython

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:

[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:

from my_module import testme

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


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

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


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.

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

# 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜