哪一个 Python 与 Perl::Tidy 最接近?

发布于 2024-09-16 03:16:55 字数 675 浏览 7 评论 0原文

来自 Perl 的我习惯于按 Cc t 根据预定义的 Perl::Tidy 规则重新格式化我的代码。现在,使用 Python,我惊讶地发现没有任何东西可以与 Perl::Tidy 的功能相提并论。 PythonTidy 1.20 看起来几乎合适,但在第一个未对齐的行(“意外缩进”)时就崩溃了。

特别是,我正在寻找以下内容:

  • 尽可能使用 PEP-8(以下各项本质上是该项的派生)
  • 将缩进制表符转换为空格
  • 删除尾随空格
  • 根据预定义的行长度分解代码就目前而言(Eclipse 风格的字符串分割和分割方法链)
  • 规范周围的空白
  • (额外功能,可选) 重新格式化代码,包括缩进。

现在,我正在检查其他人的代码并更正 pep8pyflakes 告诉我的所有内容,其中主要是“删除尾随空格”和“插入附加空白行”。虽然我知道重新缩进在 Python 中并不是微不足道的(尽管只需浏览代码并记住缩进就可以实现),但其他功能似乎很简单,我不敢相信以前没有人实现过这个功能。

有什么建议吗?

更新:我将更深入地研究PythonTidy,因为它似乎朝着正确的方向发展。也许我能找出它为什么对我呕吐。

Coming from Perl I've been used to hitting C-c t to reformat my code according to pre-defined Perl::Tidy rules. Now, with Python I'm astonished to learn that there is nothing that even remotely resembles the power of Perl::Tidy. PythonTidy 1.20 looks almost appropriate, but barfed at first mis-aligned line ("unexpected indent").

In particular, I'm looking for the following:

  • Put PEP-8 into use as far as possible (the following items are essentially derivations of this one)
  • Convert indentation tabs to spaces
  • Remove trailing spaces
  • Break up code according to the predefined line-length as far as it goes (Eclipse-style string splitting and splitting method chains)
  • Normalize whitespace around
  • (bonus feature, optional) Re-format code including indentation.

Right now, I'm going throught someone else's code and correct everything pep8 and pyflakes tell me, which is mostly "remove trailing space" and "insert additional blank line". While I know that re-indentation is not trivial in Python (even though it should be possible just by going through the code and remembering the indentation), other features seem easy enough that I can't believe nobody has implemented this before.

Any recommendations?

Update: I'm going to take a deeper look at PythonTidy, since it seems to go into the right direction. Maybe I can find out why it barfs at me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

榕城若虚 2024-09-23 03:16:55

scripts 目录中有一个与 python 一起分发的 reindent.py 脚本。

There is a reindent.py script distributed with python in the scripts directory.

红衣飘飘貌似仙 2024-09-23 03:16:55

untabify.py (Tools/scripts/untabify .py(来自 Python 源代码发行版的根目录)应该修复选项卡,这可能是阻止 Python Tidy 完成其余工作的原因。

untabify.py (Tools/scripts/untabify.py from the root directory of a Python source distribution) should fix the tabs, which may be what's stopping Python Tidy from doing the rest of the work.

穿越时光隧道 2024-09-23 03:16:55

您是否尝试过围绕 pythontidy 创建包装器? 这里有一个 Sublime 编辑器

另外, pythontidy 是否可以正确地为您分解长行?当我有一个以元组结尾的长行时,它会为元组中的每个条目创建一个新行,而不是像 PEP-8 所建议的那样在括号、方括号和大括号内使用 Python 的隐含行延续。

Have you tried creating a wrapper around pythontidy? There's one for the sublime editor here.

Also, does pythontidy break up long lines properly for you? When I have a long line that ends in a tuple, it creates a new line for every entry in the tuple, instead of using Python's implied line continuation inside parentheses, brackets and braces as suggested by PEP-8.

埖埖迣鎅 2024-09-23 03:16:55

我已经使用 autopep8 来实现此目的,并发现它很方便。

I have used autopep8 for this purpose and found it handy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文