Learning by doing#python
In the post, I’m sharing what I learned in the process with a hope that it will be helpful to someone else.
Visual Studio as ipy development environment
You can download IronPython and start to develop using your favorite text editor. However having a Visual Studio environment helps. Fundamentally it involves:
- Download and install ironpython
- Download and install Visual Studio SDK
- Run IronPython Integration Sample from ‘C:\Program Files\Visual Studio 2005 SDK\2007.02\VisualStudioIntegration\Samples\IronPythonIntegration’
Designing Windows Forms
With Visual Studio Integration, designing of Windows Forms is a simple drag and drop operation. Of course code generated by VS may not be as elegant as some Python purists like; but it works well.
Calling unmanaged code from IronPython
If you are into IronPython, you’ll definitely come across VoidSpace, who has a huge collection of IronPython articles.
I needed to query the active window for my application. VoidSpace discusses a solution in his pages. Better to name the namespace as a ‘UnmanagedCode’ as VoidSpace discusses in the solution. I named it as the name of the application and that created a lot of trouble and was difficult to troubleshoot.
Logging in IronPython
IPY doesn’t include standard python libraries (at least as of 1.1). So I used a NSpring .NET logging library.
Finding time difference
One can find the time difference between two time variable (=System.DateTime.Now) using timeSpent = currentEndTime.Subtract(currentStartTime) If you want to know the differences in seconds then use timeSpentinSec = timeSpent.TotalSeconds.ToString() timeSpent.Seconds() will provide only the seconds part of the timespan. TotalSeconds() provide the time difference in seconds.
Don’t forget to stop timer
I used Timer() to poll activewindow on a regular interval. However, I forgot to stop that assuming that once the form is closed all instances will be cleared. Apparently not and so the application stayed in memory even after exit. Lesson learned: stop all threads before exiting the application.