Archive for March 2010
Here are the links and references I used in my session:
I also referred to Ola Bini’s idea of fractal programming.
I suggested that there are two practical areas where experimentation with polyglot programming could be beneficial with existing applications and systems: extension and testing. By extension, I mean the ability to customise and add functionality – which is a great fit for a dynamic language like IronRuby or IronPython. Testing is also an area where dynamic languages have much to offer and I’d suggest taking a look at Ben Hall’s presentation that he gave at QCon earlier this year.
Sometimes you’ll hear the Python standard library referred to as “batteries included” – a little more info here. IronPython can also use these included batteries. As an example, today I needed to list the files in a folder. So, a simple Python script seemed like a good way of doing that (I’m sure there are better and more ingenious ways.) Here it is:
from optparse import OptionParser
def list_files(path, indent=0):
for filename in sorted(os.listdir(path)):
print " " * indent + filename
full_path = os.path.join(path, filename)
if (options.recursive) and (os.path.isdir(full_path)):
list_files(full_path, indent + 2)
parser = OptionParser()
action="store", type="string", dest="path",
help="The directory to list.")
help="Whether to list subdirectories.")
action="store", type="string", dest="output_file",
help="Directory contents will be listed to this file if specified.")
(options, args) = parser.parse_args()
path = options.path
path = sys.path
out = sys.stdout
output_file = open(options.output_file, 'w')
sys.stdout = output_file
sys.stdout = out
As you can see, the script takes advantage of optparse to process the command line arguments, sys (to get the current folder and to get access to and redirect the output of the script), os (to list the contents of a folder) and os.path (to test if a given path is a folder.) And, being IronPython, you also get a second set of batteries in the form of the .NET framework.
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.