简体   繁体   English

如何为 flake8 格式化 Django 设置文件

[英]How to format Django setting file for flake8

I am kinda obsessed with formating my python code with flake8.我有点痴迷于用 flake8 格式化我的 python 代码。 However, I cannot find a good way to solve E501 (line too long x > 79 characters) in settings file of Django.但是,我找不到解决 Django 设置文件中 E501(行太长 x > 79 个字符)的好方法。

First it was like this (4xE501):首先是这样的(4xE501):

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

and then I came up with this:然后我想出了这个:

AUTH_PASSWORD_VALIDATORS = [{
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    }, {
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    }, {
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    }, {
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

But still 'NAME':django.contrib.auth.password_validation.UserAttributeSimilarityValidator', is too long.但仍然是'NAME':django.contrib.auth.password_validation.UserAttributeSimilarityValidator',太长了。 Is there a way to format this or should I ignore this one?有没有办法格式化这个还是我应该忽略这个?

If you are obsessed with not getting this warning more than the actual looks of your code, then you can break a line of python code (without breaking it's continuity) by adding a \\ character at the breaking point:如果您痴迷于没有收到比代码的实际外观更多的警告,那么您可以通过在断点处添加\\字符来中断一行 python 代码(不破坏它的连续性):

Examples:例子:

# 1
from some_module import some_method, some_other_method, \
                        a_third_method

# 2
s = "A really very long string, which exist to mesh with your pep8" \
    " warning free obsession. Well, not anymore!!!"    

Attention: The \\ character raises an error when the line you are going to split is inside {}, [] or () , so you may simply do:注意:当您要拆分的行位于{}, [] or ()内时, \\字符会引发错误,因此您可以简单地执行以下操作:

AUTH_PASSWORD_VALIDATORS = [{
    'NAME': 'django.contrib.auth.password_validation.'
            'UserAttributeSimilarityValidator'
    }, ...

which is not that ugly considering...考虑到这并不是那么丑陋......


If you don't want the warning and you like your code as is, then you can add:如果你不想要警告并且你喜欢你的代码,那么你可以添加:

# nopep8 

at the end of every line that you want to exempt from pep8 analysis.在您希望免于 pep8 分析的每一行的末尾。

As an alternative (the following rewrite passes PEP8):作为替代方案(以下重写通过 PEP8):

[{"NAME": f"django.contrib.auth.password_validation.{name}"}
 for name in [
    "UserAttributeSimilarityValidator",
    "MinimumLengthValidator",
    "CommonPasswordValidator",
    "NumericPasswordValidator"]]

In python 2 you can use {}".format(name) rather than f"" .在 python 2 中,您可以使用{}".format(name)而不是f""

Was looking at Coding style |正在看编码风格| Django docs and found this: Django 文档并发现了这个:

An exception to PEP 8 is our rules on line lengths. PEP 8 的一个例外是我们关于行长的规则。 Don't limit lines of code to 79 characters if it means the code looks significantly uglier or is harder to read.不要将代码行限制为 79 个字符,如果这意味着代码看起来更难看或更难阅读。 We allow up to 119 characters as this is the width of GitHub code review.我们最多允许 119 个字符,因为这是 GitHub 代码审查的宽度。

Even people at Django avoid it (they also prefer flake8 for PEP8 checking).甚至 Django 的人也避免使用它(他们也更喜欢 flake8 进行 PEP8 检查)。 So, it'll be better if you make a .flake8 or setup.cfg file and type:所以,如果你制作一个.flake8setup.cfg文件并输入:

[flake8]
max-line-length = 119

If you are using VS Code....如果您使用的是 VS 代码....

1) Create folder ( .vscode ) in your Project. 1)在您的项目中创建文件夹( .vscode )。

2) Create settings.json file in folder ( .vscode ) and paste this code 2)在文件夹( .vscode )中创建settings.json文件并粘贴此代码

{
    "team.showWelcomeMessage": false,
    "editor.formatOnSave": true,
    "python.linting.pycodestyleEnabled": false,
    "python.linting.pylintPath": "C:Users/User/AppData/Roaming/Python/Python37/site-packages/pylint",
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django"
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true
}

where "python.linting.pycodestyleEnabled": false, (do FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM