views:

299

answers:

5

In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?

Well, perhaps "enforce" is too strong, more like a "recommendation".

I keep receiving patch files with mixed indentation and this is annoying... (to say the least) Python itself can tell when there is a problem, but I am searching something to do that at the editor level, like it exists for the charset.

Edit : Ok, my question wasn't clear, I am asking this because I keep receiving corrections and patches in any mix of tab/space you can imagine. I am using Mercurial as a DVCS, perhaps something exists at this level ?

+4  A: 

Look at the tabnanny module: — Detection of ambiguous indentation.

gimel
+3  A: 

This is something your editor should do for you. Most editors (try Notepad++ for example, it's free) will let you set whether hitting tab enters a tab character or a number of spaces. I'd recommend using two spaces instead of tab in all files (I find 4 is too much). Using spaces instead of tabs is better as it means that you can indent stuff using both the space & tab keys without worrying about messing up your files.

If you have files that have a mix it isn't hard to write your own script to convert tabs to spaces

pheelicks
+2  A: 

As explicited in PEP 8, never mix tabs and space. However, a file with both may just run... As it says there:

The most popular way of indenting Python is with spaces only.  The
second-most popular way is with tabs only.  Code indented with a mixture
of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python command line interpreter with the -t option, it issues
warnings about code that illegally mixes tabs and spaces.  When using -tt
these warnings become errors.  These options are highly recommended!

the solution is therefore to use as a default:

python -t my_mixed_code.py

To answer at the editor level, this depends on the editor, please specify!

meduz
There is an editor directive to set then character encoding of the file, for example "# -*- coding: utf-8 -*-" to save text as utf-8. Perhaps there is something similar to set the tab width ?
edomaur
Didn't know about the useful -t option, thanks
manifest
+1  A: 

I prefer to use tabs, since one tab is always one tab, and you can configure your editor to show a tab with the desired number of spaces. If a file is indented with spaces, it's less convenient if you want to change the indentation level.

piotr
@piotr, thanks for your opinion, but that does not answer the question.For every argument you can make for how tabs are better, I can counter it with an opposing argument.
aaaa bbbb
+6  A: 

Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest Ubuntu, it is provided by the python2.6-examples package.)

If you can set up a Mercurial hook you should be able to run all files through reindent.py.

By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.

unutbu
+1, love reindent. BTW, can you recommend s/thing similar for Javascript, HTML, CSS, ...? Not semantically crucial, but in multiauthor projects doing a lot of those I find the idiosyncratic style mixes annoying -- and I do mostly use hg these days!-)
Alex Martelli
@Alex: Oh, how I would love to offer you something intelligent, but alas, I don't have a good suggestion.
unutbu