强制“空格"或“标签"仅在 python 文件中?
问题描述
在 Python 中,是否有一种方法可以强制使用每个文件的空格或制表符缩进?
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".
我不断收到带有混合缩进的补丁文件,这很烦人......(至少可以说)Python 本身可以判断何时出现问题,但我正在寻找在编辑器级别执行此操作的方法,就像它存在一样对于字符集.
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.
好的,我的问题不清楚,我之所以这么问,是因为我不断收到您可以想象的任何选项卡/空格组合中的更正和补丁.我正在使用 Mercurial 作为 DVCS,也许这个级别存在一些东西?
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 ?
解决方案
Tim Peters 编写了一个漂亮的脚本,名为 reindent.py 将 .py 文件转换为使用 4 空格缩进并且没有制表符.here 可用,但请先检查您的发行版-- 它可能捆绑在示例或工具目录中.(在最新的 LTS Ubuntu 上,它由 python2.7-examples
包提供.)
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 LTS Ubuntu, it is provided by the python2.7-examples
package.)
如果您可以设置 Mercurial 挂钩,您应该能够通过 reindent.py 运行所有文件.
If you can set up a Mercurial hook you should be able to run all files through reindent.py.
顺便说一句,如果您使用的是 unix,那么您的系统可能还有 expand
(和 unexpand
)命令,它将所有制表符转换为空格(和空格到标签).但是,在这种情况下,我认为 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.
相关文章