IronPython how cool it is

Briefly, IronPython is an implementation of Python in the .Net runtime. This allows you access to  .Net framework goodness while programming in a dynamic language. The current stable version 2.0.1 maps to CPython 2.5.This allows me to do fun fun things like use my python project to access a c# project. follows is a python script using Pinsor and accessing a .net dll.

 

C# code:

public class Command
{
private readonly DoingStuff _stuff;

public Command (DoingStuff stuff)
{
_stuff
= stuff;
}
public void Execute()
{
Console.WriteLine(_stuff.Message);
}
}
public class DoingStuff
{
public string Message
{
get { return "From the Doing Stuff Class, I'm doing stuff"; }
}
}

 

 

python code:

from pinsor import *
import clr
clr.AddReference('csharpdemo')
from csharpdemo import *


kernel = PinsorContainer()
kernel.register( Component.oftype(DoingStuff),Component.oftype(Command).depends([DoingStuff]) )

cmd = kernel.resolve(Command)
cmd.Execute()

ouput is like:

C:ipyexmaple>”c:Program FilesIronPython 2.0.1ipy.exe” ipydemo.py

From the Doing Stuff Class, I’m doing stuff

I’m sure you’ll all asking yourselves “what the heck” so let me summarize:

  1. created c# dll
  2. placed a python script next to it named ipydemo.py
  3. in that script I imported the c# dll
  4. then I called my custom python IoC container Pinsor which is written in pure python
  5. added .Net objects to my python IoC container
  6. resolved a .Net object from my python IoC container
  7. then executed it with the expected results and dependencies.

When I did this test I stared at the screen completely stunned for a bit.  Pinsor was very easy to implement, and it has some decent abilities considering its short life and my limited time.  I doubt I could make the same thing in C# in twice as much time, and I’m better in C#.  This opens up some worlds for me.

With all this in mind has anyone had a chance to play with ironpython support in VS 2010?

Finally, I’d like to thank Micheal Foord with his blog and book IronPython In Action. I highly recommend reading both if you’re interested in quality programming, but especially if any of this intrigues you.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Ryan Svihla

I consider myself a full stack polyglot, and I have been writing a lot of JS and Ruby as of late. Currently, I'm a solutions architect at DataStax
This entry was posted in IronPython, Python. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.jphamilton.net J.P. Hamilton

    You could do the reverse in about 2 lines of C# 4.0 code. That is, import pinsor.py (just assuming that is what it’s called) into .NET and make calls on it. If memory serves me correctly, something like this:

    ScriptRuntime py = Python.CreateRuntime();
    dynamic pinsor = py.UseFile(“pinsor.py”);

    Haven’t tried it, but this seems pretty compelling.

  • http://www.lostechies.com/members/rssvihla/default.aspx Ryan Svihla

    @JP that’s gorgeous. I think with that sort of forwards/reverse interop we’ll start seeing more components build for .Net. Something that are painful in one language will be easy in another.

    Downside is if we end up with awful api’s for a majority of languages (like VB.net and lambda heavy libraries).

  • http://dougfinke.com/blog/ Doug

    Here in PowerShell, it compiles the C# on the fly, then runs it.

    $code = @”
    using System;
    public class Command
    {
    private readonly DoingStuff _stuff;

    public Command (DoingStuff stuff)
    {
    _stuff = stuff;
    }
    public void Execute()
    {
    Console.WriteLine(_stuff.Message);
    }
    }
    public class DoingStuff
    {
    public string Message
    {
    get { return “From the Doing Stuff Class, I’m doing stuff”; }
    }
    }
    “@

    Add-Type $code

    (New-Object Command( New-Object DoingStuff )).Execute()

  • http://www.lostechies.com/members/rssvihla/default.aspx Ryan Svihla

    @Doug thanks for the snippet but the point its a bit off i think?

    My post was to show a pure Python module written in CPython totally for CPython objects was usable in IronPython and could work on C# objects, with no modifications.

    Pinsor dynamically loads classes and their dependencies on the fly, calling the constructors of objects as needed. Very easy and simple to do in python and a bit of a pain in C# (.net reflection API is unfun to use).

    In summary, I can use Pythonic ways of solving problems on .Net code.