Re.Mark

My Life As A Blog

Archive for March 2010

Polyglot Programming at the AIC

leave a comment »

Thanks to everyone who attended my session on Polyglot Programming at AIC earlier today.  The ability to combine languages to achieve simpler solutions is worth consideration, although the potential downsides of adopting multiple languages need to be borne in mind.  In truth, many of us are already doing a form of polyglot programming – combining a programming language server side (such as C#) with Javascript on the client and SQL for data access.  However, this form of polyglot programming arises passively and is done because we have to and not because we have deliberately and actively selected a set of languages.  In order to be successful with polyglot programming, there are two crucial components: a platform and architecture.  The platform should provide language interoperability and the architecture should provide guidance on which languages to use and where in the architecture they are appropriate (taking into account that this guidance will evolve over time as you gather information and feedback about what really works for you and your team.)  In discussing the platform, I briefly touched on some of the features in .NET 4 and talked about the trends in language design.  I’ll be expanding on these platform themes and diving into a little more detail about .NET 4.0 at TechDays in April.

Here are the links and references I used in my session:

Nick Watts’ post on polyglot programming (I used his definition)

The definition of polyglot in the Compact Oxford English Dictionary

Neal Ford’s post on polyglot programming

Bertrand Meyer’s article on polyglot programming in Dr.Dobb’s

Information about the Babel Project at the Lawrence Livermore National Laboratory

Ted Neward’s MSDN article The Polyglot Programmer: Mixing and Matching Languages

History of Programming Languages

Hans Christian Fjeldberg’s thesis on Polyglot Programming

Dean Wampler’s presentation on Polyglot and Poly-Paradigm Programming

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.

Written by remark

March 31, 2010 at 5:15 pm

Posted in .NET, Architecture, Design, Events

Tagged with

Batteries Included

leave a comment »

Sometimes you’ll hear the Python standard library referred to as “batteries included” – a little more info hereIronPython 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:


import os
import os.path
import sys
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()
parser.add_option("-d", "--directory",
                  action="store", type="string", dest="path",
                  help="The directory to list.")
parser.add_option("-r", "--recursive",
                  action="store_true", default=False,
                  help="Whether to list subdirectories.")
parser.add_option("-f", "--output_file",
                  action="store", type="string", dest="output_file",
                  help="Directory contents will be listed to this file if specified.")

(options, args) = parser.parse_args()

if (options.path):
    path = options.path
else:
    path = sys.path[0]

out = sys.stdout
if (options.output_file):
    output_file = open(options.output_file, 'w')
    sys.stdout = output_file

list_files(path)

if (options.output_file):
    output_file.flush()
    output_file.close()
    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.

Written by remark

March 23, 2010 at 3:00 pm

Posted in .NET, Python

Iron Python at QCon

leave a comment »

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.

Gestalt

The final thing I talked about is Gestalt, which allows you to run Python (and Ruby) in the browser.  It does this by using the DLR, which is part of Silverlight – this is all encapsulated in javascript.

Written by remark

March 12, 2010 at 7:21 pm

Posted in .NET, Events, Microsoft, Python