Wednesday, December 1, 2010

Python: Doing it right the first time

Python has matured as a language now and the most prominent sign of it was it breaking backward compatibility to make space for more advanced features in the future. However, with maturity comes a slew of expected but not required conventions which have quickly developed around Python. This is a cheat sheet of good formatting tips, tricks and links for those new to the language. I have gathered during my slips and falls in using Python, and I hope these might help a newcomer avoid the no-you-are-doing-it-wrong moments too late into the project.

  1. PythonTidy: This is a nifty and updated script (v1.21 on 3rd Sept. 2010) by Lacus Veris which can do easily perhaps what you have dreaded to do on slightly older code: refactoring to meet standards! I had to do the following change on my system to make the script produce newlines in the output, but overall I was very satisfied:

    - OVERRIDE_NEWLINE = '' # 2006 Dec 05
    + OVERRIDE_NEWLINE = '\n' # 2006 Dec 05
  2. PyFlakes: It is a lightweight Python code checker (like PyChecker) which can notify you of minor by annoying python coding errors (like misspelling a variable name, forgetting to import a module, etc.) The checks are done in real time and can save some serious swearing and editor-to-terminal switches if you use it as a Vim plugin. However, it does not inform the coders of the styling violations. For that, consider PyLint (see later).
  3. PyLint: Pylint actually compiles the code and shouts out errors and standard style violations. There is a Vim plugin to do it each time you save the Python file. Working with a large script can get difficult if you :w as frequently as I do. However, one feature I really like is the code rating it shows after analyzing the code. 
  4. Pindent: This is a script which makes Python code slightly easier to understand. It adds some # end tags to the code after closing if or def blocks.
  5. Python project directory structure: I spent quite sometime trying to get this right. Jp Calderone has spelled out most of the rules here. Also, the Python documentation too has a few things to say about the matter, specially about the differences between a Module and a Package.
  6. Python and Vim: If you are a Vi fan, like me, then you would love these scripts by Marius Gedminas. They definitely make Vi a more powerful IDE for Python than any other I have seen out there.
  7. Style Guide for Python Code (PEP 8): This is the coding standard style book for Python: a must read before anyone writes a serious project. There are some things which the Python documentation skips perhaps because of brevity, perhaps because these are not required conventions or perhaps just because programmer are lazy, but this PEP is comprehensive and well thought out. This document is what is used for the default definition in PyLint and PythonTidy's default styling.
 If I had known these before, my initial attempts of serious projects would have fared much better. I hope they help you out.

~
musically_ut

    No comments: