Thursday, September 4, 2008

Emacs usecases 1 - Python

Previous in series: 0 - Intro
Next in series: 2 - Blogging

Disclaimer: This is pretty basic and verbose stuff. If you use Emacs to develop Python regularly, take a look at the links below. Most of the rest is probably old news to you.

Update: I forgot to mention that you can run an instance of pdb (Python Debugger) inside Emacs with M-x pdb. It lets you step through the code and keeps the cursor in the code buffer at the current line being executed. You can also use pdb to execute arbitrary Python statements while the program is paused.

There's a recent post on this blog on how VIM is the ultimate Python IDE. I have to say, after two weeks of using Emacs for editing exclusively, that Emacs is just way too feature-rich compared to VIM. There's a dedicated Python major-mode (yup, you guessed it, python-mode), provided in python.el. Then there's the inferior-python-mode, for running the Python interpreter inside Emacs. And of course you still have all the power of Emacs (some of which I probably still don't know about).

The Emacs wiki lists some possible problems, but I found the python-mode distributed with Emacs 22 to work flawlessly. Some other links you may find useful:

python-mode

Indentation in Python is very important, so it's worth noting that Emacs handles Python indentation extremely gracefully. It's worth to note that the tab character means eight spaces to Python. However, python-mode defaults to spaces-only, 4 spaces per level indentation, the Python standard. When you ask Emacs to make a new, indented line, it checks what level of indentation you really need. In most cases that's just the same as on the current line, but lines ending with a colon are indented one level deeper, while a return reduces the depth. Of course you have smart tabs, meaning that deleting an indentation doesn't remove one space, but a whole level of indentation. Additionally the Tab key doesn't increase indentation, and doesn't insert a tab character, but instead cycles the current line through the available indentation levels, showing information about the level in the minibuffer.

There are several useful shortcuts available when editing Python code. You can explore most of them via the menu *shudder* so I'm not going to list them all here. Other than the usual C-j the only one I use regularly is C-c C-c, which compiles the current buffer - evaluates it, to be accurate. It does so in an inferior Python process, which takes us to...

inferior-python-mode

Having the ability to run a Python interpreter inside Emacs is pretty useful in itself - you don't have to switch between windows to test that new piece of code. But it's better than just that. When you evaluate a buffer with C-c C-c (or a region, for that matter), it gets sent straight to the inferior Python process. You can directly access everything in the evaluated file in the interpreter. That's great for testing new functionality.
When things break, they break gracefully: if there's a problem during the evaluation, you get the same messages you would get when running the code. If you want hyperlinks to the position of the error, you can compile with mode-compile (see the intro for setting it up, my-generic.el in particular).

That's it for today. Watch this space for an Emacs + LaTeX post soon :)

No comments:

Post a Comment