Iron Python at QCon
I spent yesterday on the Microsoft stand at QCon 2010. I took a few Iron Python samples with me to show to those who are interested. I wanted to be able to show three things: .NET runs Python, Python extends .NET and Python runs .NET.
.NET runs Python
To show that .NET can run Python I used the Text Processing sample I’ve blogged about before. I’ve subsequently added optparse to it so that it can be driven from the command line. The point of this sample is that it uses standard Python libraries, the whole application is written in Python (there’s a little XAML to describe the UI) and runs on the DLR courtesy of IronPython.
Python extends .NET
For a simple demonstration of extending a .NET application with Python, I took the sample application described here. This application allows the user to write Python (at runtime) that interacts with the application.
Python runs .NET
The last sample was an adaptation of the code here that reads a Twitter feed. Rather than use Twitter (with all the shortened urls and abbreviations) I decided to use an RSS feed from the BBC to create an Iron Python newsreader. The code is remarkably simple:
import clr clr.AddReference('System.Speech') clr.AddReference('System.Xml') from System.Speech.Synthesis import SpeechSynthesizer from System.Xml import XmlDocument, XmlTextReader xmlDoc = XmlDocument() xmlDoc.Load("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml") spk = SpeechSynthesizer() itemsNode = xmlDoc.DocumentElement.SelectNodes("channel/item") for item in itemsNode: print item.SelectSingleNode("title").InnerText news = "<?xml version='1.0'?><speak version='1.0' xml:lang='en-GB'><break />" + item.SelectSingleNode("description").InnerText + "</speak>" spk.SpeakSsml(news)
This is Python using standard .NET libraries to show how a Python programmer has the .NET framework available to them through Iron Python.