Dalke Scientific Software: More science. Less time. Products
[ previous | newer ]     /home/writings/diary/archive/2003/09/21/Python_shell

interactive Python shell

When Warren showed me PyMol a couple years ago (has it been that long already?) he showed me several things I hadn't seen before. One of was a Python interactive prompt which also supported a Tcl-ish command language. The reason is simple: cd dirname is a lot easier than import os; os.chdir("dirname").

It handle other commands too. It's effectively a macro language which converts a space-separated A B C into cmd.A("B", "C") then exec's that new code. (It does more than that, like give tab-completion, but that's the essense.) If the command doesn't fit the command syntax then it's interpreted as Python.

I didn't like it at first. It requires that users deal with two different language idioms. My opinion changed at SciPy when I saw Fernando Peréz' iPython which is another interactive shell for Python. I realized both that Warren had been right and why I thought otherwise.

These days, and for the last 6 or so years, I've been writing code meant for other programmers or code with a web front-end and no programming interface at all. I did work with some computational chemists at one site who had little Python experience, but they were still computational chemists, with some programming experience in other languages.

I have to go back to my VMD days to remember working with people with very little programming experience. We started off with a homebrew command language which I replaced with Tcl. The chemists and the physicists in the group were able to pick it up very quickly, because the verb oriented syntax was easy to understand and the threshold for doing something interesting, like load a file or display a sphere, was very low. Some of the people there ended up writing scripts which were several hundred lines long and did some pretty cool things.

My problem with Tcl was that it didn't scale up well. It was great for small scripts but when I wanted to do real coding, I went back into C++. It wasn't just for performance; Tcl didn't offer any good way to build complex data structures. I needed TclX to have a good dictionary data type! (This was back in the 7.4 days so it's improved somewhat.)

I liked Perl even more as a high-level language, but it never seemed like one I wanted to explain to my non-software-developer coworkers. At first it was a problem with Perl4 because I couldn't even return two arbitrary length lists from a functions. That improved in Perl5, but I couldn't figure out an easy way to explain references to people, much less blessing anonymous references to a hash.

When I looked at Python, I thought it was a language that both beginning programmers and experienced ones would enjoy using. For the most part, I'm correct, given the people i work with. However, there's still this barrier to entry that causes people, even those with some FORTRAN, C, and shell background, to put off learning Python, and it's more than inertia.

I now think Warren and Fernando are correct and there's a missing step between a command interface and Python's OO/imparative one. After all, when I use Python's interactive prompt I usually swap between two windows: the Python prompt and the shell. Why can't I have both at the same prompt?

I was worried about the mixing of language idioms. I'm more mollified now since I realized that any of my clients who use the prompt are people who will use the shell prompt, so there isn't another interface to learn. I also think it's pretty easy to see which language is in use so they won't be mixed up that often.

Now to install iPython and experiment with it... Err, actually, now it's time to sleep. :)

Andrew Dalke is an independent consultant focusing on software development for computational chemistry and biology. Need contract programming, help, or training? Contact me

Copyright © 2001-2013 Andrew Dalke Scientific AB