Feeds

Quick scripting for .NET with IronPython

Office benefits

Internet Security Threat Report 2014

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.

Internet Security Threat Report 2014

More from The Register

next story
Be real, Apple: In-app goodie grab games AREN'T FREE – EU
Cupertino stands down after Euro legal threats
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Bada-Bing! Mozilla flips Firefox to YAHOO! for search
Microsoft system will be the default for browser in US until 2020
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
prev story

Whitepapers

Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
How to determine if cloud backup is right for your servers
Two key factors, technical feasibility and TCO economics, that backup and IT operations managers should consider when assessing cloud backup.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.