您现在的位置是:主页 > news > 一个网站做两级三级是什么意思/日本关键词热搜榜
一个网站做两级三级是什么意思/日本关键词热搜榜
admin2025/4/30 22:36:28【news】
简介一个网站做两级三级是什么意思,日本关键词热搜榜,有需要做网站推广找我,做网站卖产品投资大嘛文章目录行宽使用空格缩进文件头注释函数docstringPylint安装Pylint在PyCharm中添加Pylint工具生成Pylint配置文件在PyCharm中使用Pylint检查代码行宽 File -- Settings -- Editor -- Code Style -- 右侧 -- Hard wrap at 设置成79。 因为PEP8 – Maximum Line Length的要求是…
文章目录
- 行宽
- 使用空格缩进
- 文件头注释
- 函数docstring
- Pylint
- 安装Pylint
- 在PyCharm中添加Pylint工具
- 生成Pylint配置文件
- 在PyCharm中使用Pylint检查代码
行宽
File -- Settings -- Editor -- Code Style -- 右侧 -- Hard wrap at
设置成79
。
因为PEP8 – Maximum Line Length的要求是79。
使用空格缩进
File -- Settings -- Editor -- Code Style -- 展开 -- Python -- 右侧 -- Tabs and Indents
,设置Tab size = 4
,Indent = 4
,Use tab character
前面的勾去掉。
为了使得缩进在各种IDE,各种文档编辑器中都显示出相同的排版效果,各规范通常要求以空格来缩进,不允许使用tab,更加不允许tab与空格混用。上面的设置是将Tab size
设置成以4个空格来代替,每一级的缩进量Indent
也是4个空格。这样我们每次敲tab键其实相当于敲了4个空格。
请注意Google风格的排版可能是要求缩进2空格。
文件头注释
File -- Settings -- Editor -- File and Code Templates -- 右侧 -- Files -- Python Script
,然后在最右侧的框里填上文件头注释模板,模板里面可以使用一些宏定义的变量。见下图
可用的宏定义变量有:
# ${PROJECT_NAME} - 项目名称
# ${NAME} - 新建文件的名称
# ${USER} - 当前用户的登录名
# ${DATE} - 当前系统日期
# ${TIME} - 当前系统时间
# ${YEAR} - 当前年份
# ${MONTH} - 当月
# ${DAY} - 当月的当天
# ${HOUR} - 当前小时
# ${MINUTE} - 当前分钟
# ${MONTH_NAME_SHORT} - 月份名称
# ${MONTH_NAME_FULL} - 月份名称
设置图中所示模板后,再创建新的文件时,文件头自动添加以下内容:
# -*- coding: utf-8 -*-
# test_config - 项目名称
# test - 新建文件的名称
# by - 当前用户的登录名
# 2021/2/13 - 当前系统日期
# 12:56 - 当前系统时间
# 2021 - 当前年份
# 02 - 当月
# 13 - 当月的当天
# 12 - 当前小时
# 56 - 当前分钟
# 2月 - 月份名称
# 二月 - 月份名称
我自己一般只使用# -*- coding: utf-8 -*-
作为文件头注释。一是因为某些规范的要求,特别是PEP8 - Source File Encoding;二是因为非utf-8编码情况下,代码可能不识别汉字。
函数docstring
File -- Settings -- Tools -- Python Integrated Tools -- 右侧 -- Docstrings -- docstring format
,选择一个你喜欢的,或者你的规范要求的docstring风格。如果选择Plain,那docstring就是空白的。
添加方法是在函数下方敲三个单引号或者双引号,然后回车即可。
我个人比较喜欢Numpy风格,风格如下:
def fun(input1, input2):"""Parameters----------input1input2Returns-------"""pass
如果能在变量后面再添加个冒号就更好了,可惜docstring貌似没法自定义。
Pylint
Pylint可以用来检测代码的风格和错误,本文档仅介绍最简单的操作,更详细的内容可以参考下面网站:
https://pylint.readthedocs.io/en/latest/user_guide/index.html
安装Pylint
如果Python使用Anaconda安装的话一般情况下已经有pylint。
如果使用的Python环境没有pylint的话,进入cmd中使用如下命令安装:
pip install pylint
在PyCharm中添加Pylint工具
打开File -- Settings -- Tools -- External Tools
,在这个界面中点击上方的加号:+,出现一个Create Tool界面,内容填写如下:
填完以后点OK就可以了,下面是文字版本方便复制。
Name随便填个能自己认识的名字就行):
pylint
Description(写不写无所谓):
A Python source code analyzer which looks for programming errors, helps enforcing a coding standard and sniffs for some code smells.
Program(必须填写准确,可以点击右侧的文件夹图标选择路径):
注意D:\Program\Anaconda3是我的Python环境目录,你应当改成你自己的。
D:\Program\Anaconda3\Scripts\pylint.exe
Arguments(也可能叫Parameters,最后的$FilePath$是必需的,其他都可选):
--rcfile=E:\Python\pylint\pylint.conf $FilePath$
Working Directory($FileDir$是必需的):
$FileDir$
生成Pylint配置文件
在cmd中进入D:\Program\Anaconda3\Scripts
目录(如果已经将此目录加入环境变量则不用进入该目录),执行下面命令:
pylint --generate-rcfile > pylint.conf
此时按照pylint的默认配置在cmd的当前目录下生成一个文件pylint.conf,将该文件拷出放在一个合适的地方,比如通过上面配置中的Arguments可以看出我放在了E:\Python\pylint\目录下。
如果在Arguments中不指定–rcfile=E:\Python\pylint\pylint.conf,那么每次检查都会提示No config file found, using default configuration,然后使用pylint的默认配置文件,但事实上我们不会总是想要使用默认配置,因为可能跟需求不符合。比如pylint.conf中有下面参数:
# Maximum number of characters on a single line.
max-line-length=100# Maximum number of lines in a module
max-module-lines=1000
PEP8规范中建议每行字符数小于等于79,虽然以现在的显示屏宽度来看有点太短了,但是PEP8以及很多公司确实是按照这个建议来要求的,然而默认配置中是100,所以需要根据需求改一下。下面那个1000也是经常需要修改的参数。
除此之外,在DESIGN部分也有很多让人感觉尴尬的参数,可以根据你的规范或者需求进行更改:
[DESIGN]# Maximum number of arguments for function / method
max-args=5# Maximum number of attributes for a class (see R0902).
max-attributes=7# Maximum number of boolean expressions in a if statement
max-bool-expr=5# Maximum number of branch for function / method body
max-branches=12# Maximum number of locals for function / method body
max-locals=15# Maximum number of parents for a class (see R0901).
max-parents=7# Maximum number of public methods for a class (see R0904).
max-public-methods=20# Maximum number of return / yield for function / method body
max-returns=6# Maximum number of statements in function / method body
max-statements=50# Minimum number of public methods for a class (see R0903).
min-public-methods=2
在PyCharm中使用Pylint检查代码
在PyCharm界面中通过以下方式使用Pylint对当前页面的代码进行检查:Tools -- External Tools -- Pylint
Pylint的输出有五类:
Output:Using the default text output, the message format is :MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGEThere are 5 kind of message types :* (C) convention, for programming standard violation* (R) refactor, for bad code smell* (W) warning, for python specific problems* (E) error, for probable bugs in the code* (F) fatal, if an error occurred which prevented pylint from doingfurther processing.
从下面网站找了一段输出的例子,输出会告诉我们“问题类别,行号,描述”等信息,然后对着一条一条改就是了,改到10分。
https://pylint.readthedocs.io/en/latest/tutorial.html
************* Module simplecaesar
simplecaesar.py:16:19: C0326: Exactly one space required around assignmentencoded=encoded + letters[x]^ (bad-whitespace)
simplecaesar.py:1:0: C0111: Missing module docstring (missing-docstring)
simplecaesar.py:5:0: C0103: Constant name "shift" doesn't conform to UPPER_CASE naming style (invalid-name)
simplecaesar.py:6:0: C0103: Constant name "choice" doesn't conform to UPPER_CASE naming style (invalid-name)
simplecaesar.py:7:0: C0103: Constant name "word" doesn't conform to UPPER_CASE naming style (invalid-name)
simplecaesar.py:8:0: C0103: Constant name "letters" doesn't conform to UPPER_CASE naming style (invalid-name)
simplecaesar.py:9:0: C0103: Constant name "encoded" doesn't conform to UPPER_CASE naming style (invalid-name)-----------------------------------
Your code has been rated at 6.32/10
不过有的时候Pylint会报一些非常2B的错误,比如Pylint检测不到某些第三方库时可能报出大量错误,这种不用在意。