在将代码重构为小于maxlinelength后撤消pep8 maxlinelength格式

2024-04-25 22:01:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在攻击模式下使用pep8(参数-a)。通常,当我有这样的代码超过指定的字符长度时:

plt.legend(handles=[indrnn_plot, lstm_plot, gru_plot, rnn_plot, irnn_plot], loc=0)

pep8格式如下:

^{pr2}$

我发现自己在编写代码时不断地重构代码,有时会更改代码,以便单行表示不再超过我的最大字符长度:

plt.legend(
    handles=[
        indrnn_plot,
        lstm_plot,],
    loc=0)

对于我来说,这种情况发生得太频繁了,以至于我花了太多时间去清理pep8拒绝“撤销”的混乱。为了让上面的代码回到一个单行表示法,我手动删除了换行符和空白符以达到以下目的:

plt.legend(handles=[indrnn_plot, lstm_plot,], loc=0)

我需要我的代码简洁(就行数而言),但仍要遵循pep8格式准则,以避免超过最大行长度。换句话说,当字符长度不再大于最大字符长度时,我需要用更少的行来表示重构后的代码。在

是否有一个参数可以传递给pep8,或者有一个技巧可以让像上面这样的重构代码自动格式化回单行表示形式?在


Tags: 代码参数plot格式模式plt字符loc
1条回答
网友
1楼 · 发布于 2024-04-25 22:01:43

它看起来可能有你需要的内置功能,但我看不到一个预先构建的选项

>>> import autopep8
>>> autopep8.join_logical_line("""plt.legend(
...     handles=[
...         indrnn_plot,
...         lstm_plot,],
...     loc=0)""".decode('utf-8'))
u'plt.legend( handles=[ indrnn_plot, lstm_plot,], loc=0)\n'
>>> print(u'plt.legend( handles=[ indrnn_plot, lstm_plot,], loc=0)\n')
plt.legend( handles=[ indrnn_plot, lstm_plot,], loc=0)
>>> print(len(u'plt.legend( handles=[ indrnn_plot, lstm_plot,], loc=0)\n'))
55

因此,您需要按逻辑行遍历文件,检查join是否小于或等于79个字符,然后替换它。在

相关问题 更多 >

    热门问题