thoughts = rand()

Monday, September 24, 2007

IronPython - My .NET Exploration Tool

Whenever I need to try out little things in .NET or find out how a .NET API works, I wrote C# files, compiled and executed them. Many a times I felt the compile-execute-recode cycle to be too much for experimenting. I wanted an interactive console where I can type code and see how it works. IronPython seems to be an answer for me. The advantage of IronPython over other Python implementations supporting .NET is that IronPython has been embraced by Microsoft. I think they have hired the IronPython creator too.

So this is what I did with IronPython when trying to explore the Security classes in .NET:

>>> from System import *
>>> from System.IO import *
>>> from System.Security.Cryptography import *
>>> stream = FileStream("C:/report.txt", FileMode.Open, FileAccess.Read)
>>> sha1 = SHA1Managed()
>>> Convert.ToBase64String(sha1.ComputeHash(stream))

Well if you are still wondering what the above code does, it just computes the SHA1 of a file called 'report.txt' in my C: drive. It prints the SHA1 as a Base64 formatted string (Base64 is a human readable formatting of binary data).

I believe Windows Power Shell enables one to do such .NET explorations too. I don't want to jump into that until I get Vista.