Feeds

Quick scripting for .NET with IronPython

Office benefits

Boost IT visibility and business value

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.

5 things you didn’t know about cloud backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Sin COS to tan Windows? Chinese operating system to debut in autumn – report
Development alliance working on desktop, mobe software
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
(Not so) Instagram now: Time-shifting Hyperlapse iPhone tool unleashed
Photos app now able to shoot fast-moving videos
prev story

Whitepapers

A new approach to endpoint data protection
What is the best way to ensure comprehensive visibility, management, and control of information on both company-owned and employee-owned devices?
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Maximize storage efficiency across the enterprise
The HP StoreOnce backup solution offers highly flexible, centrally managed, and highly efficient data protection for any enterprise.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.