Quick scripting for .NET with IronPython
Hands on, part 1 While Ruby and Groovy seem to get all of the attention, there's one scripting language that has been around for a lot longer and that has quietly been picking up in popularity over the long run. That language is Python, and, according to the TIOBE Programming Community Index Python was the language of the year for 2007.
While development of the core version of Python (often called CPython - because it's coded in C) continues apace, there are also versions that target the two big software platforms: Jython targets the Java platform (using the Java Virtual Machine and inter operating with native Java code), and the other is IronPython, which targets Microsoft's .NET Framework and was discussed at Microsoft's Mix 08 conference last week.
The current version of IronPython (version 1.1.1, released at the end of January 2007), uses the .NET Common Language Runtime, while the next major release will be built on top of the Dynamic Language Runtime, which runs on top of the CLR. In this two parter we'll take a look at IronPython and the IronPythonStudio, which is built on Visual Studio 2008, officially launched last month, starting with a look at the language itself.
The simplest way of getting IronPython is to download the latest release from CodePlex. Installation is as simple as downloading and unpacking the zip. Once done, navigate to the appropriate directory and simply enter ipy to start the interactive Python interpreter:
C:\IronPython-1.1.1>ipy IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433 Copyright (c) Microsoft Corporation. All rights reserved. >>> print 'hello world' hello world
Now, old Python hands will be wondering about the availability of some of the standard Python modules that they know and love. IronPython doesn't include all of these by default, so the first thing to do in that case is to point it at that library. To do this, just append a pointer to the standard library to the sys.path. At the ipy prompt just enter the following:
>>>import sys >>>sys.path.append(r'c:\Python25\lib')
As a test let's grab hold of an old (and very, very useful) module called glob (which is used for Unix-style file/path name expansions) that isn't included in IronPython. Now that we have the sys.path set, it's just a case of doing the import - we'll do that and use it to grab a list of msi files in the c:\download directory:
>>>import glob >>>glob.glob(r'c:\download\*.msi') ['c:\\download\\IronPythonStudio.msi', 'c:\\download\\python-2.5.1.msi']
So far so good, we can use Python like we used to. But the real meat of IronPython is access to the .NET Framework and all that it contains. To illustrate this we're going to write some code that mixes and matches .NET libraries with CPython modules, and that is going to involve grabbing hold of Excel as well. This application will take a path name and list some details of all of the Excel workbooks that it can find. Now, to do this requires that we have the Primary Interop Assemblies for Microsoft Office in place. These are included by default with Office 2007, but need to be installed separately for Office 2003 - full details are included here.
Rather than continue to use the interactive shell, it's time to write some code so create a text file called wkbs.py. The first thing is to put in place the code we need to grab the path from the command line, or else fall back on a suitable hard-coded default. It also lists the files that it finds for us:
import sys sys.path.append(r'c:\Python25\lib') import glob import os.path from System.IO import File, FileInfo if __name__ == '__main__': if len(sys.argv)==2: wkbdir=sys.argv else: wkbdir=r'c:\spreadsheets\*.xls*' wkbs=glob.glob(wkbdir) print wkbs
Run this by typing ipy wkbs.py <your path> at the command prompt.
Not very interesting yet, and no sign of .NET. So let's make use of some libraries from the System.IO package. Going back to the code let's define a function that lists the workbook name and last accessed date. Add the following to the end of wkbs.py:
def wkbInfo2(w): print 'Name: %s, Last Accessed: %s' % (FileInfo(w).Name, File.GetLastAccessTime(w)) return w names=[wkbInfo2(wkb) for wkb in wkbs ] print 'Total files : %d' % len(names)
This prints out the name and last accessed timestamp of each file, along with a count of found files at the end.
But we want more than this, we want to know the number of worksheets and the name of each sheet as well. For this we need to open each workbook in Excel, walk the worksheets collection and then exit cleanly.
def wkbInfo3(w): ewb=excel.Workbooks.Open(w) print "Name: %s, Number of worksheets: %s" % (ewb.Name, ewb.Worksheets.Count) for bk in ewb.Worksheets: print '\tSheetName: ' + bk.Name ewb.Close(False) ewb=None return w import clr import System print '\nLinking to Excel...' clr.AddReference("Microsoft.Office.Interop.Excel") import Microsoft.Office.Interop.Excel as Excel excel = Excel.ApplicationClass() names=[wkbInfo3(wkb) for wkb in wkbs ] print 'Total files : %d' % len(names) excel.Quit() excel=None
Note the house keeping in that code, explicitly setting the workbook and application classes to None to ensure that references are released. Failure to do so means that you can end up with a rogue (and invisible) Excel instance running in the background after the program has finished executing.
As should be clear, IronPython provides a good environment for quickly knocking up scripts that can make full use of existing .NET classes and packages. Even relatively tricky things, like grabbing hold of an Office application, can be accomplished easily enough.
The examples so far, though, are console applications coded in a simple text editor. What if you want something in the way of a GUI? And you want more than notepad? In part two we'll look at IronPythonStudio to see what it's got to offer.®