Some options for reindenting (some of) my existing Python code

August 15, 2016

In my entry on how I'm probably going to change my style of indenting Python code, I said that it was at least a bit tempting to reformat existing code to be PEP-8 compatible (probably using the 'if I have to touch it at all, it gets reindented' approach). This leaves me with the question of how to do that.

In an ideal world I could simply load code into GNU Emacs and say 'okay, my indent level is going from 8 spaces to 4 spaces, lay out everything again with this in mind'. Unfortunately I don't think Emacs's Python mode offers this feature (and it's actually slightly harder than it looks). The one time I tried to sort of fake this with Emacs features that I could find, it blew up in my face in a painful way.

(I'm not familiar with vim's Python smarts, but maybe it has something to do this.)

The great big hammer is yapf. This will do the reindentation, but with the side effect of reformatting all of my code according to its idea of proper style. Having looked at its output, I don't entirely agree with it and I feel that it shuffles code and comments around more than I'm happy with. I could give in (for the same reason I gave in with gofmt), but I'm also not convinced that yapf is going to become the one true style that people write to.

Yapf compares itself to autopep8 and pep8ify. It looks like at least pep8ify can be told to only change indentation and not touch anything else, so perhaps that is the easiest approach. On the other hand, I might find that one or the other (or both) of these make some additional formatting changes that I'm okay with. I'd have to try them.

There's also a reindent Python package, and a version of this appears to be part of the tools that are packaged with Python 3. This says it's specifically about reindenting code and almost nothing more, which is just what I want. If the PyPi version still works in Python 2, this is probably a good thing to check out first.

Finally, there is the yak shaving option of writing my own version of this. On the one hand, it doesn't seem too complicated and in theory it would do exactly what I want. On the other hand, there are all sorts of corner cases with continued lines and so on, and if there are existing tools that do it right (or right enough) I should probably just use one of them instead of indulging my vague instinct to write some code.

(If I was all fired up to write this sort of code it might be different, but I'm not. 'Write my own' is just a vaguely grumpy reflex.)

(As usual, writing this entry has caused me to do enough research that I now know that I have a lot more options than I thought I did.)


Comments on this page:

If all your python code is indented by tabs, then in vim, you can set tab width to 4 spaces, and you can replace all indent tab to spaces using one command, and done.

Written on 15 August 2016.
« Code alone can tell you the what but it cannot tell you why
My ambivalent view on Vim superintelligence, contrasted with GNU Emacs »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Aug 15 00:39:41 2016
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.