Thursday, January 19, 2006

ToolBox: Monitoring Gasolin Prices through The Internet

I've posted a gas price web monitoring tool on "CodeProject". The tool monitors the gas price at any gas station of your choice around Great Toronto Area (GTA) of Canada. When the price you set has been reached, the tool will notify you by popping up a window and playing some music. You may configure the tool to monitor gas prices automatically when your computer is started. You also can use it as a simple web browser.

The tool is free software, written in C# for Visual Studio 2005 on Microsoft.Net Framework 2.0. The entire source code is delivered free of change. If you just want to use the tool, all you need is Framework 2.0 Re-distributable. I've also built a Windows Installer for the tool, with which you can install the tool by a click. The Installer is not posted on the "CodeProject" site, but you can contact me if you want the Installer.

Wednesday, January 18, 2006

How Python and C/C++ Work Together?

Recently, I have had a chance to work on integrating Python modules with C/C++ applications. Python is a powerful interpreted (and scripting) language, like Perl and PHP. Along with the available tools and libraries, Python makes a good choice for modeling and simulation developers. Best of all, it's free and the tools written for Python programmers are also free. For more details on the language, visit the "official website".

Now, you have got some modules written in Python by others and you want to use them in C/C++ applications. You are experienced in C/C++, but fairly new to Python. You might wonder if you can find a tool to convert them to C code, like the conversion from FORTRAN. The answer is no. The only choice you're having is to work on bringing Python and C/C++ together.

There are two general approaches to integrating Python with C/C++. One is embedding, in which you use Python/C API library to call Python modules directly, as you call any other C/C++ functions. I'm not going to say more about this approach. For more details, see my articles on "CodeProject".

In contrst, the second approach allows Python and C/C++ modules to run separately and talk to each other through an IPC mechanism. This really is a "black-box" approach, in which your C/C++ code does not call Python modules at all. Instead, they communicate with each other through message exchanges.

The IPC approach requires that you convert your Python modules into executables and libraries so that you can run them as independant applications. There are several such tools that can help, one of which is called "cx_Freeze", which is portable across platforms. I've tested it on Windows XP, RedHat Linux and SGI Irix 64. When implementing this approach, you need to do two things:

1) Write IPC interfaces for both Python and C/C++ modules. I found it fairly simple to implement a memory-mapped files (MMAP) IPC on both Python and C/C++ sides.

2) Convert each Python module to an executable with associated libraries. Then you are ready to run Python and C/C++ modules as separate processes and let them talk to each other through the IPC.

Personally, I prefer the first approach as it gives us a "white-box" control over Python modules. One drawback about the first approach is that it demands more intimate knowledge of the Python implementation.