Our software is designed for integration into complex sequences controlled by external programs or scripts. This integration is facilitated by the following features:
- All our programs may be launched from the command prompt or may be called by other programs with pass parameters.
- Although the user interfaces are largely interactive, our programs feature input buffered through text scripts. These scripts may be created or modified by external programs, giving wide latitude for passive or active control loops.
Interpreter languages represent an efficient option for creating shells that call our programs and take care of global data organization. In previous posts (Automatic solution control with Perl and Making movies with the AMaze programs) I discussed the Perl language and gave examples of its application. After recently comparing notes with my son, I feel that Perl may not be the best choice. We both had similar problems as occasional Perl user. Returning to the language always seemed like starting at square zero. Its syntax and control structures are arcane, and the documentation is spotty. Perl would appeal to a computer scientist because it is clever and succinct. On the other hand, succinctness can be a curse to applied scientists and engineers. I often found that I could not understand Perl codes that I had written a few months earlier. Minimalist syntax might be useful for writing Forth programs on a ZX81, but it makes no sense in an age of cut-and-paste editors and terabyte hard disks.
My son suggested Python as an alternative. Like Perl, Python is available at no charge for Windows, Linux and Apple computers. As a test, I decided to see how much effort it would take to duplicate the movie-making script described in a previous post. The task proved to be relatively easy. I downloaded and installed the programs, read the introductory documentation and duplicated the function of the script in about 3 hours. Here is a link to down my resulting script (with a .txt suffix added to avoid download problems):
These are the critical lines for launching our software from Python:
import subprocess ProgName = "phiview" ScriptPrefix = "pvscript" ... Command = ProgName + " " + ScriptPrefix subprocess.Popen(Command).wait()
They create the command line string “phiview pvscript” and then call the program as a subtask. The .wait() directive ensures that only one instance of phiview.exe runs at a time. In less than a minute, the script generates 100 graphics files showing the electrostatic potential distribution at different positions in the solution space.
In comparison to Perl, I feel that Python is the better choice for the occasional user working on applied problems for the following reasons:
- Python syntax has more intuitive appeal. Special characters to denote data types (“$”, “&”, “@”,….) are not required. It is unnecessary to put a vestigial semicolon at the end of each line, a frequent source of error. I like the idea of defining control loops by indentation since it closely matches my style of programing in FORTRAN.
- Python scripts may be run from an interpreter window. In this case, the script should have a name of the form fprefix.py. You can open the script with a built-in editor with syntax highlighting. A script is built by adding material and making changes in the editor and pressing F5 at any time to check the operation. Printed results and error messages appear in the interpreter window and the error position is marked in the editor. This simple arrangement is quite effective for building and debugging programs. When the script is complete, you simply rename it as fprefix.pyw. Then it can be run as a background task without opening a window.
- Python has a comprehensive and well-organized help system that can be called directly from the interpreter. This makes it easy to find the structure of a control sequence, and easy to look it up again when you have forgotten it. In contrast, I had to buy two tomes from OReilly to get started with Perl. Long searches through the books were necessary each time I returned to the language.
You can get Python for Windows at http://python.org/download/. There is a question of whether to download Python 2.7 or 3.1.2. The latter is the development version with advanced features, The 2.7 version is still active and is compatible with many current resources available on the Internet. Considering that 2.7 has far more features than I would ever use, I decided to be conservative and use it. Installation is largely automatic. There are two operations you may need to do by hand:
- Add a desktop icon to run the interpreter (c:\python27\pythonw.exe).
- Add c:\python27 to your path. In XP, run the Control Panel. Run System and click on the Advanced tab. Click on Environmental variables, highlight the PATH variable and click on EDIT. Go to the end of the long string and add “;C:\PYTHON27″ (be sure to include the semicolon). Exit by clicking OKs. The modified path variable will not take effect until the next time you log in. It’s not necessary to restart the computer. Just Log Off the current user and log in again.
