tags:

views:

158

answers:

2

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!

+2  A: 

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

AutomatedTester
Yes. I actually have something like that working. But my question is a bit different. I am working on a library that is a drop in replacement for PyCrypto (so that paramiko can work without having to using Ironclad). I am using the PythonModule attribute to make the imported module look more like a real python module (first line of the code snippet)...rather than an imported c# dll. Perhaps I'm being too complicated and I should just go ahead without the attribute...as you say that already works. Maybe Dino Viehland will comment.
djlawler
I should have thanked you for this answer! It's pretty good and would have saved me a bundle of time a little while ago. I think my question was not as clear as I would have liked it to be!
djlawler
+1  A: 

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.

Jeff Hardy
PyCrypto is a bit funny yes. I started out to just 'wrap' BouncyCastle's c# library, but then I ran into their implementation of Counters for the CTR/SIC mode of block encryption and I had to forget about that. I've got something that is basically the PyCrypto interface grafted onto nearly unchanged routines ripped out of BouncyCastle. But it passes all of PyCrypto's tests so even though I'm still cleaning it up I'll pop it out on bitbucket soon.
djlawler
By the way....thanks for the answer Jeff. I think you fully understand the issue. It seems that you can import python packages fine in IronPython and also use namespaces in c# dll's in a 'package' sort of way. You can also take a c# dll and make it look like a module. However either I have not found it, or there is no way to emulate a package with submodules in c#. I guess we can hope that someone will confirm this or give us some idea how to make it work in the next couple of days...If not I will select your answer.
djlawler
I'm going to pick this as an answer. I've found that using this:[assembly: PythonModule("my_module", typeof(test.MyModule))]in the c# program and some creative 'import as' statements in the python part of the programs will get me to where I want. The good news is that the c# classes then show up looking like python modules from IronPython. The bad news is that they show up in "__builtins__" and have odd "__name__" and "__file__" values. Still I am able to run the complete test suite so I think I'll be ok. I'll try asking this on the IronPython mailing list sometime soon.
djlawler