English 中文(简体)
我该如何在 python 评论中格式化一个长的 url, 并保持 PEP8 符合 PEP8 。
原标题:How should I format a long url in a python comment and still be PEP8 compliant
  • 时间:2012-05-24 14:47:47
  •  标签:
  • python
  • pep8

在块注释中,我想引用一个长度超过80个字符的 URL。

显示此 URL 的首选常规是什么?

我知道 bit. ly 是一个选项, 但 URL 本身是描述性的。 缩短它, 然后用嵌套的评论描述缩短的 URL 似乎是一个错误的解决方案 。

最佳回答

不要打破url:

忽略某项准则的其他一些良好理由:

  1. When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP. ...

资料来源:

# A Foolish Consistency is the Hobgoblin of Little Minds [1]
# [1]: http://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds

You can use the # noqa at the end of the line to stop PEP8/pycodestyle/Flake8 from running that check. Should also avoid warnings in your IDE.

# [1]: http://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds # noqa
问题回答

来自PEP8

但最重要的是:知道何时前后不一 -- 有时风格指南就是不适用。 当有疑问时, 请使用你最好的判断。 看看其他例子, 并决定什么是最好的。 并且不要犹豫询问!

违反特定规则的两个良好理由:

  • When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules.

就我个人而言,我会使用这个建议, 而不是在您的评论中 留下完整的描述性 URL 给人们。

您可以在行末使用 来阻止 PEP8/Flake8 执行检查。 PEP8允许通过 :

特殊情况并不特别,无法打破规则。

我说别管它了...

PEP20 :

特殊情况并不特别,无法打破规则。

虽然实用性胜过纯洁

在粘贴到浏览器时, 快速复制/ 粘贴 url 比较实际, 然后可以删除线条 。

如果您使用flache8:

"""
long-url: http://stackoverflow.com/questions/10739843/how-should-i-format-a-long-url-in-a-python-comment-and-still-be-pep8-compliant
"""  # noqa

将# noqa 添加到全部的笔记本工作上, 但这意味着您会失去对整个笔记本工作的反省, 这样您就可以忽略其它问题 。

若您想将您的 noqa 缩小到仅长线, 您只能将其添加到长线, 但# noqa 在用 sphinx 构建时出现在文档中 。

在此情况下, 您可以建立自定义的自定义自动博士处理方法 。 < a href="https:// stackoverflow.com/ questions/ 26534184/can- sphinx- ignore- certain- tags- in- python- docstrings" > 这个答案 。

这是我改编的版本

from sphinx.application import Sphinx
import re

def setup(app):

    noqa_regex = re.compile( ^(.*)ss#snoqa.*$ )

    def trim_noqa(app, what_, name, obj, options):
        for i, line in enumerate(lines):
            if noqa_regex.match(line):
                new_line = noqa_regex.sub(r 1 , line)
                lines[i] = new_line

    app.connect( autodoc-process-docstring , trim_noqa)
    return app




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...