强制“空格"或“标签"仅在 python 文件中?

Enforce quot;spacesquot; or quot;tabsquot; only in python files?(强制“空格或“标签仅在 python 文件中?)
本文介绍了强制“空格"或“标签"仅在 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.

这篇关于强制“空格"或“标签"仅在 python 文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to find element by part of its id name in selenium with python(如何使用python在selenium中通过其id名称的一部分查找元素)
NoSuchElementException: Message: Unable to locate element while trying to click on the button VISA through Selenium and Python(NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素) - IT屋-程序员软件开发技术分
Selenium not able to click on Get Data button on using Python(Selenium 在使用 Python 时无法单击“获取数据按钮)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python(selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击
Selenium Compound class names not permitted(不允许使用硒化合物类名称)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium(selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试使用 selenium 单