Categories
Python

.NET Framework and IronPython

I am a Python programmer and a Python enthusiast. I love the expressive syntax of the language. I have used it for automating my tasks and developing web sites. I have developed PyQT desktop apps as well. Today I am going to focus on IronPython, another cool distribution of Python programming language. It’s based on the .NET platform introduced by Microsoft. It aims at providing a dynamic runtime language for the .NET platform.

I am using genuine Windows 7 on my Notebook PC. I’ve been trying to install Microsoft Visual Studio 2008 for the last couple of days. But I failed every time. I guess it’s happening because I am using Windows 7 64bit edition. After repeated failure with VS2008, I decided to try .NET platform with Python. So, I went to IronPython home page (see link at the bottom) and browsed the downloads section. By default, it prompted me to get IronPython 2.6 for .NET framework 4.0. I didn’t have v4.0 of .NET installed, so I downloaded the one for .NET 2.0 instead. Tell you what, .NET 4.0 is more than 40 MB download and I am running a bit tight on available data transfer 😛

The setup was pretty easy. I ran the MSI file and it installed for me. But in case, you don’t have .NET 2.0 installed, you’ll need to get that. (See link at the bottom).

After installing IronPython, I noticed a cool looking IPY (IronPython :P) console. It works just like the interactive python shell. Remember, the version I installed didn’t put IPY in the system path. So, typing “ipy ” on the command line will simply fail. Please go to: Control Panel\System and Security\System > Advanced System Setting > Environment Variables. Then append the path of IronPython to the “PATH” variable. Now restart “cmd” 😀 Now, you can execute any Python script with IPY.

Here’s a sample “Hello World!” GUI I wrote to test IPY:

You have to import the “clr” module first. Add a reference to the .NET namespace you want to use. Then use that namespace just like a Python module.

Cool. Isn’t it? So, why should you use IronPython? Good question. Most windows users will by now have .NET framework 2.0 or later installed. The IPY installer is only 6 MB. The end user of your application will need to download less and install less. Please remember PyQT and wxPython are pretty heavy. JAVA requires a huge installation as well. IronPython cares both about you and your users. You get a fantastic expressive language and an excellent platform. Your users get the ease and comfort of installation. Also remember, with Mono, IPY is now cross platform. So your programs will run on other operating systems as well.

With IronPython you can do so many things — develop ASP.NET web sites, work with Robotics and Silverlight, develop games with XNA and build robust programs. So if you’re a Python programmer, come on and mix up .NET and your python wizardry to create something awsome 😉

Resources / Links:
IronPython Home Page
IronPython Download
.NET framework 2.0 Redistributable Package
IronPython Cookbook

Categories
Linux Python

Python Script to Monitor Laptop Battery Charge (Ubuntu/Linux Mint)

I got my laptop two days ago. It’s a Compaq Presario CQ42-220TU. Battery backup is around 3 hours. I am using Linux Mint which is a variation of Ubuntu. Just a few minutes ago I wrote a Python script that monitors the battery charge and if it’s discharging, it gives a notification with remaining percentage of charge every 5 minutes.

Here’s the source codes for the curious minds 😉

Download:http://code.google.com/p/masnun/downloads/detail?name=battery.py

For convenience, I added the script to my start up applications. So whenever I log in to my lappy, it gets auto started. If you want to use this script as well, save the above source code in a file. Let’s assume you saved it in a file named “battery.py” in your home folder. Now give it execution permission. Right click on the file. Go to properties. In the “Permissions” tab, tick the option saying “Allow executing file as program”. Close the dialog box.

Now go to “Preferences” menu from Sytem menu (Ubuntu) or Mint Menu (Linux Mint). Go to Startup Applications. Click Add. Type in a name and comment. In the command field, type the full path of the file. Eg. /home/<_username_>/battery.py.

Restart your PC. 🙂

If you can’t get it working, please provide feedback here or catch me on IM. My contact details are available on https://masnun.com.

Categories
Python

My First Gedit Plugin

I am now using Ubuntu 10.04. In an older post of mine, I described how you can use the PHP_Beautifier tool with Gedit to code php in a fashion. But the major drawback was that a shell output used to popup every time you used the external tool. I was looking for a solution and even after a while I could not find one.

I knew Gedit supports plugins and you could write plugins in C & Python. Blessed that I know some Python, I went through the plugin API and soon realized that gedit plugin development was not much hard. I took a sample snippet, scratched and played around and finally achieved what I wanted.

I named it “Shell Hider”. All it does is to disable the entire bottom panel so that the shell output don’t stay long. In fact, the shell window will popup for a second and then hide itself. On a fast machine, you probably won’t see the popup at all!

Download it from here: http://bit.ly/cQeTMx

Enjoy!