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: ,

JAOO Århus 2009

by DotNetNerd 8. October 2009 17:56

Actually I had not planned to go to JAOO this year, but first my good colleagues who run ANUG struck a good deal where Mads Torgersenand Oren Eini alias Ayende Rahienwould give a special talk in the evening for the usergroup. This was simply too good to pass up. After all, how often do you get the chance to see one of the Danes from the C# language team and Ayende – who is the guy behind tools like NHibernate and Rhino.Mocks.

On top of the “Geek night” as it was coined, I ended up going Wednesday for the .NET track as well. To my gain a colleague who already had a ticket could not go, so I was lucky enough to get the offer to go in his place.

It ended up being a very interesting night and day I think – with my only small complaint to Trifork, who did brilliantly, being that the .NET track was held in a building that was below par compared to “Musik huset” where the rest was held.

Key points for me was hearing Mads, Ayende twice and Udi Dahan. Madses talked about what it is like to work in the C# team, and the considerations they have to take. This was really interesting and its hard not to be a bit hit by national pride that there are actually two Danes on a team like that. 

Ayende mostly talked about his own projects with the talks focused around NH Prof and NHibernate. It is impressive to see him with a keyboard, even though I had hoped the focus of his talks would have been a bit wider. The one general issue he talked about was at the ANUG talk where he talked about concepts and features. On the upside in the day session we did get to see some of the stuff he has added to NHibernate where especially NHibernate Search looked quite interesting.

Udi Dahan gave my personal “talk of the day”, because I think he got the focus of the talk right, and managed to make it fun and interesting. The subject was pretty broad being “Scalability, Availability and Reliability”, but he made it pretty concrete without making it about a single product. The focus was on how a one way messaging architecture based around transactional queues could solve a lot of the typical issues that we run into as developers. Actually the fact that he has worked on NServiceBus did not come up until the talk was basically over and it was just named as a possible solution amongst a bunch of others.

NServiceBus will be the focus of the next ANUG meeting where my colleague Lasse Eskildsen will  be showing a bunch of good examples on how it can be used. So this was the perfect fuel to light up my interest for the subject. Besides what I have already written about there is one more topic that tickled my taste buds, which is Scala. It does not take much to get me going when it comes to programming languages, so maybe it is just me, but I think Ill have to take a look even though my focus right now is more on IronPython.

 

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