JP's Laboratory

Posts / Code / Photos / Videos / Contact

Better Python code

Written: 20130301

Up until now my development environment has been quite primitive. I have usually just used Gedit or Pico/Nano for writing Python code. Sometimes also Emacs, but then mostly as a plain editor. My new year promise this year was to try to have a more up to date development environment. The promise got a little delayed, but now it seems to be up and running.

One of the reasons for me to having such a primitive development environment, is that it is easy to waste a lot of time on setting up and tweaking these environments, instead of actually writing code. But I've come to realize that having a good setup for coding can actually improve the quality of the code. So I finally did something about it.

Here are some important documents on Python coding style. If you don't know them already, you should read or browse through them.

Setting up vim

I'll give Vim a try this time - it was suggested by a co-worker, and I kind of like his setup.

PEP 8 quickfix list

I use this plugin occasionally, but you might want just to install Syntastic, which is described in the next section.

Follow the guide here to install the plugin:

http://www.vim.org/scripts/script.php?script_id=2914.

You'll need to install PEP 8 also.

When in Vim run the pep8 check by pressing F5 (the defalt key). You will be presented by the "Quickfix List" mode of Vim. You can navigate the errors using the keys.

This gives you a neat overview of the problems, but it feels a little cumbersome to navigate back and forth between the quickfix list and the code window itself.

Syntastic

Syntastic is an automatic syntax checker for Vim. It supports many languages, and will actually detect which syntax checkers are installed on you system, and use the correspondingly.

The Syntastic plugin can be obtained here along with install instructions.

You'll need PEP 8 and pylint installed.

Syntastic will highlight error in your code. The checks will be made each time you load or save the file. You can navigate the code, and the error will be displayed at the bottom of the console.

Selecting a color scheme

Okay selecting a color scheme is very much personal taste, but it is important that you select a good color scheme, which you feel comfortable with, as you often might spend several hours programming in a row.

I've selected a color scheme called Solarized. It has very nice and balanced colors. However I couldn't used it on Vim on the iTerm terminal, as the OS X version that I have doesn't support more than 256 colors. So I have to use MacVim for this reason. But I don't mind that so much, because I find it quick to navigate using the touch pad, which is close to my thumb when using my MacBook Pro.

Here is a screen shot of MacVim with the Solarized color scheme:

MacVim with Solarized color
    scheme.
comments powered by Disqus