tags:

views:

3383

answers:

6

I have some python code that have inconsistent indentation, there is a lot of mixture of tabs and spaces to make the matter even worse even space indentation is not preserve. The code works as expected but its difficult to maintain.

I'm looking for a tool that will fix the indentation (like html tidy but for python) and won't break the code :)

+1  A: 

Using vim, it shouldn't be more involved than hitting Esc, and then typing..

:%s/\t/    /g

..on the file you want to change. That will convert all tabs to 4 spaces. If you have inconsistent spacing as well, then that will be more difficult.

Ben Hughes
inconsistent spacing also
Shay Erlichmen
can you track down the original coder(s) and apply enhanced interrogation techniques? There is nothing worse than inconsistently indented code.
Ben Hughes
@Ben The inconsistent indention is the least of our problems from that guy :)
Shay Erlichmen
A: 

Try emacs. It has good support for indentation needed in Python. Please check this link http://python.about.com/b/2007/09/24/emacs-tips-for-python-programmers.htm

cheers

Andriyev
python-mode is nice for writing Python, but nothing at the link describes how to *fix* indentation in an existing file.
ephemient
M-x untabify will turn tabs into space in emacs
Paul Hildebrandt
+8  A: 

If you're using Vim, see :h retab.

                                                        *:ret* *:retab*
:[range]ret[ab][!] [new_tabstop]
                        Replace all sequences of white-space containing a
                        <Tab> with new strings of white-space using the new
                        tabstop value given.  If you do not specify a new
                        tabstop size or it is zero, Vim uses the current value
                        of 'tabstop'.
                        The current value of 'tabstop' is always used to
                        compute the width of existing tabs.
                        With !, Vim also replaces strings of only normal
                        spaces with tabs where appropriate.
                        With 'expandtab' on, Vim replaces all tabs with the
                        appropriate number of spaces.
                        This command sets 'tabstop' to the new value given,
                        and if performed on the whole file, which is default,
                        should not make any visible change.
                        Careful: This command modifies any <Tab> characters
                        inside of strings in a C program.  Use "\t" to avoid
                        this (that's a good habit anyway).
                        ":retab!" may also change a sequence of spaces by
                        <Tab> characters, which can mess up a printf().
                        {not in Vi}
                        Not available when |+ex_extra| feature was disabled at
                        compile time.

For example, if you simply type

:ret

all your tabs will be expanded into spaces.

You may want to

:se et  " shorthand for :set expandtab

to make sure that any new lines will not use literal tabs.


If you're not using Vim,

perl -i.bak -pe "s/\t/' 'x(8-pos()%8)/eg" file.py

will replace tabs with spaces, assuming tab stops every 8 characters, in file.py (with the original going to file.py.bak, just in case). Replace the 8s with 4s if your tab stops are every 4 spaces instead.

ephemient
+46  A: 

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:

Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files. Also ensure the last line ends with a newline.

Have a look at that script for detailed usage instructions.

Alex Martelli
Unfortunately, it's not part of the normal Python install on Debian and Ubuntu; it's split out into the `python-examples` package.
ephemient
Alex Martelli
Works like a charm, I left the question open for a while so you can enjoy more up votes.BTW, is there a way to force constant indention per file? I want python to allow only 4 spaces per indentation level and fail the parsing otherwise. If the answer is yes then I will open a new question.
Shay Erlichmen
@Shay Erlichmen: Try "python -tt yourscript.py"
nosklo
@nosklo's right, -tt gives errors on inconsistent tab usage (however it does tolerate _consistent_ tab usage, 3-space indent vs 4, etc, so it's still more permissive than @Shay would want).
Alex Martelli
Fedora/RedHat/CentOS users should install the "python-tools" package.
Cristian Ciupitu
Just to add to ephemient's comment: once `python-examples` is installed, you'll find reindent in `/usr/share/doc/pythonX.X/examples/Tools/scripts/reindent.py`.
Larry Hastings
+2  A: 

There is also PythonTidy (since you said you like HTMLTidy) It can be found here: http://pypi.python.org/pypi/PythonTidy/1.16 It can do a lot more than just clean up tabs though. If you like that type of thing it's worth a look.

Paul Hildebrandt
+1  A: 

If you're lazy (like me), you can also download a trial version of Wingware Python IDE, which has an auto-fix tool for messed up indentation. It works pretty well. http://www.wingware.com/

Yansky