IronPython: Dot Net adventures with Python

However, in the course of his research he stumbled upon the fact that this new Microsoft technology is in fact good for dynamically typed languages and such languages can coexist in the Dot Net space with the more famous statically typed languages. So what does Hugunin do? He ports Python on to Dot Net. And IronPython was born!

That is a brief history of the beginnings of IronPython. In 2004 it was one of the most promising projects in Python space. However, Hugunin was tempted to join the Common Language Runtime (CLR) team at Microsoft by fall 2004, which meant that the GPL-compliant licensed IronPython was over.

However, Hugunin moved to Microsoft and revived the project as a Microsoft sponsored community effort licensed under Shared Source License for IronPython. Not many in the Open Source and Free Software Community have liked this move. Many have not minced words in expressing their anguish at forums and mailing lists on the World Wide Web.

Politics apart, IronPython is essentially an implementation targeting CLR. IronPython can be used to compile Python programs into bytecode (IL) that will run on either Microsoft's .NET or the Open Source Mono platform. 

Just like Cpython, IronPython includes an interactive interpreter and transparent on-the-fly compilation of source files, akin to standard Python. You can also make both DLLs and exe files using the static compilation option in IronPython.

Some reasons for IronPython’s claim to fame include:
1. IronPython is reported to be between 10% and 300% faster than CPython or regular Python;
2. IronPython is very small in size and packs power in small bits and bytes; and
3. IronPython can be used to access all Dot Net libraries.

In this article I will explore IronPython in detail and try to understand how well it works with the Dot Net Framework.
The architecture of IronPython consists of a tokenizer, a parser, a code generator and a library of built-in functionality. The code generator is used to convert Python code into Intermediate Language. 
Essentially, whenever the interpreter encounters a line of code, it gets automatically converted into IL, which the Dot Net framework understands.
Getting Started
The latest version of IronPython (at the point of going to press) is the pre-alpha release of 0.7.6. To run, IronPython 0.7.6 requires Dot Net Framework version 2.0 to be installed on your system. It is also advisable to have the latest Dot Net 2.0 Beta SDK and Runtime, to get the program up and running. You can dump the extracted files in a folder on your hard drive. You need to locate the bin folder in the IronPython 0.7.6 folder and enter the command IronPythonConsole.exe to get the interpreter window.

C:\IP\IronPython-0.7.6\bin>IronPythonConsole.exe
IronPython 0.7.6 on .NET 2.0.50215.44
Copyright (c) Microsoft Corporation. All rights reserved.
>>>


Python all the way!
IronPython is Python all the way! In fact, the Python shell looks very much like the CPython shell. The first thing I tried was to add a couple of numbers.

>>> 7+3
10


Once I knew that the Python shell was working, I was tempted to try the much beaten track!

>>> "Hello World!"
'Hello World!'
>>> print 'Hello World!'
Hello World!
>>>


How about a little counter program?

>>> list=range(10)
>>> for item in list:
... print item,
...
0 1 2 3 4 5 6 7 8 9>>>
()


IronPython is written using C#, which is one of the new languages that was born along with Microsoft Dot Net. 

I tried exploring IronPython further. You can run Python files from the Command Prompt using the IronPythonConsole.exe by simply typing the file name.

C:\IP\IronPython-0.7.6\bin>IronPythonConsole.exe new2.py

To my dismay, I found that most of the Python modules are yet to be implemented!

IronPython.Objects.PythonImportError: No module named os
at input_14.Run(Frame frame)
>>> import zipfile
IronPython.Objects.PythonImportError: No module named zipfile
at input_15.Run(Frame frame)

This is because IronPython development is still at a primitive stage. However, you now have the whole Dot Net library stack at your disposal. For example, in the code snippet below we have imported the System.Console class from Dot Net 2.0.

>>> import System.Console
>>> System.Console.Writeline(“Hello World!”)
>>> Hello World!


Handling Dot Net classes and libraries works through the sys module in which two new methods have been added.

The first one is LoadAssemblyByName. This method will load a Dot Net class or package into the IronPython namespace. See code 1.

>>> import sys
>>> sys.LoadAssemblyByName("System.Windows.Forms")
>>> import System.Windows.Forms as WinForms
>>> WinForms.MessageBox.Show(“Hello World”, “MyWindow”)


Code1

This will pop up a new window as in figure 1.

That was achieved using four lines of code. In C# it would have taken roughly twice the number of lines.

There is no real help provided with IronPython. There is an examples folder with a few files, which is not very helpful. There are two ways to find help. Check the host of books and web sites with content on Python. You can learn Python very easily.

The other thing you need to do is to go through Microsoft SDK help files, to find out more on SDK. If you can read the documentation and also understand Python programming, you can move ahead with IronPython very fast.

I explored further. I hacked a C# program and rewrote the simple Form creation in IronPython. See code 2 below and refer figure 2.

>>> import sys
>>> sys.LoadAssemblyByName("System.Windows.Forms")
>>> sys.LoadAssemblyByName("System.Drawing")
>>> from System.Windows.Forms import *
>>> from System.Drawing import *
>>> form1=Form(Text="Hello World", HelpButton= True)
>>> form1.Show() 


Code 2

Now let us see how we can create a button and have that displayed (code 3).

>>> b=Button(Text ="Click Me",Location= Point (30,30), Size= Size(100,50
>>> form1.Controls.Add(b)
>>> form1.ShowDialog()


Code 3

You will get a button.

Let us see what the program would have looked like in C#. I have hacked a C# program that looks more or less like the one given in Code 4.

using System;
using System.Drawing;
using System.Windows.Forms;

public class form1 : Form
{

Button b = new Button();
}

public form1()
{
b.Location = new Point(30, 30);
b.Text = "Click Me";
b.Size = new Size (100,50);
Controls.Add(button1);

}
static void Main() 
{
Application.Run(new form1());
}


Code 4.

Readers can understand by now that a dynamically typed language like Python can make programming a lot simpler, even by Dot Net standards.

To add more functionality, I decided to add a small function to the button Click Me. One of the beauties of Python is that you can reference functions and methods by assignment (code 5).

>>> form1=Form(Text="Hello World", HelpButton= True)
>>> b=Button(Text ="Click Me",Location= Point (30,30), Size= Size(100,50))
>>> def push (data, event):
... WinForms.MessageBox.Show ("OUCH")
...
>>> b.Click +=push
>>> form1.Controls.Add(b)
>>> form1.ShowDialog()


Code 5.

Once you click the button in the form, a Message Box is displayed.

What we essentially did was to pass the function push, which displays the MessageBox, to the Click method in the object b. You can add similar controls very easily in IronPython.

I would like to discuss more on implementing Web Forms (ASP.net) using IronPython. However, this is something I will take up in a later issue.

Last edition we had seen Jython, which is a mature implementation of Python, for the Java platform. IronPython in comparison is still in its primitive stages. However, it is expected that within a few months we will see a full fledged and stable 1.0 version, which hopefully has better support for more native Python modules. Currently, only a handful, including sys, time, re and math are implemented. There may be at least 50 useful Python modules that need to be rewritten.

However, a startling fact that impressed me was that IronPython was very quick. In fact, it proved to be faster than similar C# applications. This is indeed an improvement compared with Jython, which is still much slower than Java.

Finally, all Python developers, especially those programming on Windows or Dot Net platform, will celebrate the day an IronPython plug-in will be available for VisualStudio.net.




Added on June 28, 2007 Comment

Comments

Post a comment