强制执行“空格"或“标签"仅在python文件中? [英] Enforce "spaces" or "tabs" only in python files?
问题描述
在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)编写了一个漂亮的脚本,名为此处可用,但请先检查您的发行版-可能已捆绑在示例"或工具"目录中. (在最新的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.
这篇关于强制执行“空格"或“标签"仅在python文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!