Saturday, August 22, 2009

Catching up with Devhawk: Using Python libraries from C#, the HawkCodeBox and a world tour

Devhawk, otherwise known as Harry Pierson, is the program manager for IronPython on the Microsoft dynamic languages team. He is a prolific, even more than me, has a whole bunch of interesting IronPython related posts that I've fallen behind on.

The first set of posts is about building a hybrid Python / C# application, specifically using the awesome Pygments syntax highlighting library from a .NET application.
Harry has been working on a syntax highlighting plugin for the Windows Live Writer desktop blogging tool. He's using Pygments via IronPython 2.6 Beta 2 to do the highlighting, and barring some problems with the import magic Pygments does (known by the IronPython team as “Harry’s Pygments Import Bug”) it works fine with IronPython.
C# 4.0 introduces a new feature, the dynamic type, that will make embedding and interacting with dynamic languages from C# much easier. If you want to use IronPython with .NET versions before 4.0 (which isn't even released yet) you'll have to use alternative techniques.

To create a custom content source for Windows Live Writer you need an on disk assembly with a static type and custom attributes; which means it can't be done directly from IronPython. In this blog entry Harry introduces the posts where he solves this problem.For his hybrid IronPython / C# Windows live writer plugin Harry wants to ship Pygments in the form of a single compiled assembly.
IronPython ships with a script named pyc for compiling Python files into .NET assemblies. However, pyc is pretty much just a wrapper around the clr module CompileModules function. I wrote my own custom script to build the Pygments assembly from the files in a the pygments and pygments_dependencies folders.
The custom script to compile the Pygments package into a single assembly weighs in at only 14 lines of IronPython code. In the rest of the entry Harry discusses the pros and cons of compiling Python packages like this and how he ensures he has included all the dependencies.
In this blog entry Harry writes a generate_html function, in Python, which uses Pygments to do the syntax highlighting. He also goes over how he includes this function in his plugin (as an embedded resource) and sets up the IronPython runtime:
Now that I’ve got Pygments and its dependencies packaged up in an easy-to-distribute assembly, I need to be able to call it from C#. However, if you pop open pygments.dll in Reflector, you’ll notice it’s not exactly intuitive to access. Lots of compiler generated names like pygments$12 and StringIO$64 in a type named DLRCachedCode. Clearly, this code isn’t intended to be used by anything except the IronPython runtime.

So we better create one of those IronPython runtime thingies.
This post is the real meat of the series. Having set up the IronPython runtime he shows the C# for looking up and calling the Python functions.
If you’re working with dynamic languages from C#, the ObjectOperations instance than hangs off the ScriptEngine instance is amazingly useful. Dynamic objects can participate in a powerful but somewhat complex protocol for binding a wide variety of dynamic operation types. The DynamicMetaObject class supports twelve different Bind operations. But the DynamicMetaObject binder methods are designed to be used by language implementors. The ObjectOperations class lets you invoke them fairly easily from a higher level of abstraction.
The rests of the Devhawk posts (yes there are more) cover a range of different topics:
The IronPython project homepage on Codeplex includes a wiki. As the wiki can only be edited by members of the IronPython team, who have a lot on their plate, there is a lot of useful information that could be there that isn't...

This is where you come in. Codeplex now has an "Editor Role" feature and if you are interested in helping you can become a wiki editor for IronPython:
Until now, the only way to give members of the community the ability to edit the wiki also gave permission to edit work items, check in source code and make releases. We’re still working on getting Microsoft at large to understand the benefits of community collaboration aspect in open source, but in the meantime we just can’t give those permissions to people off the team. However, we would love to have contributions to our documentation wiki. With the new Editor Role, we’ll be able to grant wiki editor access without any of the other permissions.
Windows Presentation Foundation (WPF) is the shiny new user interface for Windows (not in Mono unfortunately). One of the touted benefits of WPF over its aged predecessor Windows Forms is extensibility, but unfortunately this doesn't seem to apply to some of the standard controls like the WPF TextBox. Fortunately Ken Johnson has a workaround that he wrote about on the CodeProject.

Harry would like a syntax highlighted REPL (interactive interpreter) for plugging into .NET applications, so he has started to write one - the HawkCodeBox.
Harry is traveling the world, speaking about IronPython and other topics. Here is a summary of his itinerary (so far):
  • Danish University Tour, Sept 7-11
Harry is presenting at four different universities in Copenhagen in four days.
I’ll be visiting Aalborg University, Aarhus University, University of Southern Denmark and University of Copehhagen as well as delivering a TechTalk at the Microsoft Development Center Copenhagen, which is Microsoft’s biggest development center in Europe. I’ll primarily be delivering my Iron Languages introductory talk “Pumping Iron”, but there’s also some interest in language development on the DLR so I’ll be talking on that topic as well.
Not specifically speaking on IronPython, Harry's talk topic is called “Not Everything is a new Nail() : How Languages Influence Design”.
Talks not yet confirmed, but Harry has three talks submitted.
The big annual Python conference.
I finally feel like I might have something interesting to present at PyCon this year. ... We already have one announcement that I think is pretty significant lined up and might have a second depending on how hard I can push LCA and management between now and then. Talk proposals are due October 1st, so any suggestions would be appreciated!
It looks like the folk in Denmark are eagerly awaiting his arrival:
Jeg har inviteret Harry Pierson til Danmark i hele uge 37, det betyder at vi kan komme hele landet rundt!

>>> ironPythonTechTalk = TechTalk()
>>> ironPythonTechTalk.Who
Harry Pierson (
>>> ironPythonTechTalk.When
Friday, 11th September 2009 09:00 - 11:00
>>> ironPythonTechTalk.Where
Microsoft Development Center, Copenhagen Frydenlunds Alle 6, 2950 Vedbaek
>>> ironPythonTechTalk.WhoIsInvited
You are and did I mention that it's free
>>> ironPythonTechTalk.DoesThisCompile
Ohh yes indeed
>>> ironPythonTechTalk.CantMakeIt
Does these cities and days work better for you!?
{'Aalborg Uni. ': 'Monday, time/schedule: TBD',
'Copenhagen Uni. (DIKU)': 'Thursday, time/schedule: TBD',
'Syddansk Uni.': 'Wednesday, time/schedule: TBD',
'Aarhus Uni.': 'Tuesday, time/schedule: TBD'}
>>> ironPythonTechTalk.StillCantMakeIt
Can\'t help you, sorry...
You can also sign up for his talk at the Microsoft Developer Center in Copenhagen:
Get an introduction to Python on .NET from Harry Pierson (

The TechTalk will be held in English. It is open for everybody and participation is free.

When: September 11 2009 from 09:00-11:00
Where: Microsoft Development Center Copenhagen, Frydenlunds Allé 6, 2950 Vedbæk

Registration deadline: September 7 or as long as seats are available.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.