Webdev: the times, they are a changin'

Dave Smith dave at thesmithfam.org
Thu Apr 19 07:13:13 MDT 2012

On Apr 19, 2012, at 4:22 AM, Stuart Jansen wrote:

> Not perfect, but good enough for most lines. I don't think it's possible
> to get vim to do a great job of indenting or folding because it was
> designed to work best with C-like languages.

I've tried several python vim indenting plugins, and they all fall short on at least these trivialities (some are much worse):

1. It indents lines below a comment ending in ":" (as if it were an if statement, but it's just a comment)
2. It folds in the middle of a doc string that happens to look like Python code (say, example usage)
3. It breaks Vim's Shift+e command due to weird shell errors about Django environment variables and other stuff
4. Auto-indent of whole blocks is hopelessly broken (this is more inherent to Python unfortunately).

That is my biggest beef with Python: You simply cannot auto-indent. And before you say, "Well, the language naturally indents, so there's no need", may I offer this preemptive retort:

If I have an existing block of code that I want to wrap in an "if" statement, I have to manually indent the whole block (thank goodness for Vim's Ctrl+v and Shift+i). In C++, I would simply wrap the block in an "if" with its curly braces, and then auto-indent using Vim's "%" curly brace matching command and the "=" auto-indent command.

If I attempt the same with Python, I'm likely to change the flow control in the block. Blarg!

I've come to two conclusions about writing Python code:

1. Pylint is not optional.
2. Unit tests with 100% code coverage are not optional.

Those two commandments are necessary just to give you the ground level confidence that your code is syntactically valid and free from even the most basic errors (like using a name before its assigned, nonexistent module, and even some syntax errors, for example).

So there you go, free advice worth what you paid for it (about 5 minutes of your time, so not cheap--sorry).


More information about the PLUG mailing list