IronPython – integrating with .NET

by DotNetNerd 25. October 2009 18:04

One of the great things about IronPython compared to other Python implementations is that is running on the .NET platform and was written in C#. This lets the developer use all the stuff he already knows from using .NET and he will have the possibility of writing components in C# that can extend or be used from IronPython. I regular Python writing extensions would be done I C which is enough to scare a big part of the developer community away. On top of that the IronPython syntax and type system is pretty close to C#, so if you are already programming in C# you will have a fairly easy time getting started with IronPython.

Doing integration of an existing language into a framework is always going to be a challenge. Some concepts in Python map fairly well to the existing .NET languages, while others require that you learn how to get about doing things from IronPython as they will require a little deeper understanding. A good deal of these challanges are covered in “The dark corners of IronPython”. The most basic thing you need to know though, is how to access classes and navigate namespaces from IronPython.

Namespaces are a concept that maps pretty well between C#/VB and IronPython, because Python has a similar concept which is called modules. In Python a module is a file which can contain any number of classes and which can be grouped into packages, which are actually just folders on the file system.

To gain access to using a class it is imported by writing an import statement - in roughly the same way as namespaces are used to group classes in C# and VB. This means that doing a import in IronPython that allows us to access a .NET class is very straight forward. (Iron)Python does have differences though. One difference is that a package contains a file named __init__.py, where you can write code that is run when the package is imported. Secondly and probably more importantly in Python the import allows you specify exactly which classes to import instead of the whole namespace/module.

from System.IO import Directory, File

This line of code will allow us to use the Directory and File class of the System.IO namespace. Writing a * instead of the filenames will mean that all classes in the namespace are imported. This is regarded as somewhat of a bad practice in the Python community though. This is basically what you need to know to start accessing .NET classes from IronPython like this:

from System.IO import File, Directory, FileInfo 
from System.Text.RegularExpressions import Regex

files = Directory.GetFiles("D:\MyFiles")
for i in range(0,files.Length):
    file = files[i]
    fileInfo = FileInfo(file)
    reg = Regex("^\d\d[\s-]+")
    res = reg.Replace(fileInfo.Name, "")
    print res

Tags: ,

Who am I?

My name is Christian Holm Diget, and I work as an independent consultant, in Denmark, where I write code, give advice on architecture and help with training. On the side I get to do a bit of speaking and help with miscellaneous community events.

Some of my primary focus areas are code quality, programming languages and using new technologies to provide value.

Microsoft Certified Professional Developer

Microsoft Most Valuable Professional

Month List

bedava tv izle