此处将为大家介绍关于如何在python注释中格式化长网址,并且仍符合PEP8的详细内容,此外,我们还将为您介绍关于Pycharm配置autopep8让Python代码更符合pep8规范、Pycharm
此处将为大家介绍关于如何在python注释中格式化长网址,并且仍符合PEP8的详细内容,此外,我们还将为您介绍关于Pycharm 配置 autopep8 让 Python 代码更符合 pep8 规范、Pycharm配置autopep8教程,让Python代码更符合pep8规范、python – 如何在PyCharm中格式化多行TODO注释?、python3 格式化代码工具(autopep8 black)的有用信息。
本文目录一览:- 如何在python注释中格式化长网址,并且仍符合PEP8
- Pycharm 配置 autopep8 让 Python 代码更符合 pep8 规范
- Pycharm配置autopep8教程,让Python代码更符合pep8规范
- python – 如何在PyCharm中格式化多行TODO注释?
- python3 格式化代码工具(autopep8 black)
如何在python注释中格式化长网址,并且仍符合PEP8
在块注释中,我想引用一个长度超过80个字符的URL。
显示此URL的首选约定是什么?
我知道bit.ly是一个选项,但URL本身是描述性的。对其进行缩短,然后使用嵌套的注释描述缩短的URL,这似乎是一个糟糕的解决方案。
Pycharm 配置 autopep8 让 Python 代码更符合 pep8 规范
一、何为 pep8?
- PEP 8 官方文档 -- Style Guide for Python Code
- PEP8 中文翻译(转)
二、Pycharm 中配置 pep8
Pycharm 本身是有 pep8 风格检测的,当你敲得代码中不符合规范时,会有下划波浪线提示。如何让代码修改为符合规范,去掉这些难看的波浪线呢?下面介绍步骤:
2.1 安装 autopep8
cmd 窗口输入:pip install autopep8
2.2 在 Pycharm 中安装 autopep8 配置
- Tools→Extends Tools→点击加号
- 配置项
- Name:Autopep8(可以随便取)
- Tools settings:
- Programs:autopep8 (前提是你已经安装了哦)
- Parameters:--in-place --aggressive --aggressive $FilePath$
- Working directory:$ProjectFileDir$
- 点击 Output Files→添加,在对话框中的:Regular expression to match output 中输入:$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
- autopep8 在 pycharm 中的使用:在 Pycharm 编辑其中新建一个 python 文件,编辑一些不符合 pep8 风格的代码;将鼠标放在该文件的编辑器中→右键→External Tools→点击 Autopep8。这样你的代码就符合 pep8 的风格了。如下图所示
参考
- 【机器学习入门 ——2】Pycharm 安装 autopep8
来自 <https://segmentfault.com/a/1190000005816556>
Pycharm配置autopep8教程,让Python代码更符合pep8规范
一、何为pep8?
- PEP 8官方文档 -- Style Guide for Python Code
- PEP8中文翻译(转)
二、Pycharm中配置pep8
Pycharm
本身是有pep8
风格检测的,当你敲得代码中不符合规范时,会有下划波浪线提示。如何让代码修改为符合规范,去掉这些难看的波浪线呢?下面介绍步骤:
2.1安装autopep8
cmd窗口输入:pip install autopep8
2.2在Pycharm中安装autopep8配置
- Tools→Extends Tools→点击加号
- 配置项
- Name:Autopep8(可以随便取)
- Tools settings:
- Programs:`autopep8` (前提是你已经安装了哦)
- Parameters:`--in-place --aggressive --aggressive $FilePath$`
- Working directory:`$ProjectFileDir$`
- 点击Output Filters→添加,在对话框中的:Regular expression to match output中输入:`$FILE_PATH$\:$LINE$\:$COLUMN$\:.*`
- autopep8在pycharm中的使用:在Pycharm编辑其中新建一个python文件,编辑一些不符合pep8风格的代码;将鼠标放在该文件的编辑器中→右键→External Tools→点击Autopep8。这样你的代码就符合pep8的风格了。如下图所示
最后
博客链接:
- Michael翔
参考
- 【机器学习入门——2】Pycharm 安装 autopep8
python – 如何在PyCharm中格式化多行TODO注释?
# Todo: Multiple errors can be wrapped inside an exception. # WfcApiException should do recursive error checking to locate # and store an arbitrary number of nested errors.
不幸的是,PyCharm只将第一行识别为Todo评论.以下任何行都被视为标准Python注释.
在PyCharm中格式化多行Todo注释的正确方法是什么?
解决方法
PyCharm does not支持多行Todo,另一种选择是使用多行字符串
''' Todo foobar foobar '''
这将不会像#Todo foo那样突出显示Todo,但它会突出显示字符串突出显示的其余代码.
你也可以试试
# Todo ----------------- # Todo foobar this # Todo comment # Todo comment # Todo comment # Todo ------------------
如果你有一个特别厚实和重要的Todo笔记.
python3 格式化代码工具(autopep8 black)
工具一:autopep8
1. 关于pep8格式
https://www.python.org/dev/peps/pep-0008/
Github地址 :https://github.com/hhatto/autopep8
2. 安装
pip3 install autopep8
根据自身情况 看是否需要 加入环境变量
3. 使用
原python代码格式
# cat test.py
import math,sys;
def example1():
####This is a long comment. This should be wrapped to fit within 72 characters.
some_tuple=( 1,2,3,'a' );
some_variable={'long':'Long code lines should be wrapped within 79 characters.','other':[math.pi,100,200,300,9876543210,'This is a long string that goes on'],'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,20,40000,500000000,60000000000000000]}}
return (some_tuple,some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3( object ):
def __init__ ( self,bar ):
#Comments should have a space after the hash.
if bar : bar+=1; bar=bar* bar ; return bar
else:
some_string = """
Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
return (sys.path,some_string)
格式化后的代码:
将格式化后的代码重新写入文件中
# autopep8 --in-place --aggressive --aggressive test.py
或直接看格式化效果,不覆盖原有的代码
autopep8 --aggressive --aggressive test.py
查看格式化后代码:
# cat test.py
import math
import sys
def example1():
# This is a long comment. This should be wrapped to fit within 72
# characters.
some_tuple = (1,'a')
some_variable = {
'long': 'Long code lines should be wrapped within 79 characters.','other': [
math.pi,'more': {
'inner': 'This whole logical line should be wrapped.',some_tuple: [
1,some_variable)
def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}
class Example3(object):
def __init__(self,bar):
# Comments should have a space after the hash.
if bar:
bar += 1
bar = bar * bar
return bar
else:
some_string = """
Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
return (sys.path,some_string)
工具二:black
black号称不妥协的代码格式化工具,为什么叫不妥协呢?因为它检测到不符合规范的代码风格直接就帮你全部格式化好,根本不需要你确定,直接替你做好决定。它也是 requests 作者最喜欢的工具之一.使用非常简单,安装成功后,和其他系统命令一样使用,只需在 black 命令后面指定需要格式化的文件或者目录就ok。
1. 安装
pip3 install black
根据自身情况 看是否需要 加入环境变量
2. 使用
这是一款小而美的工具,它并不是完全按照 PEP8 规范来格式化,比如默认每行代码的字符数是88个,当然你可以通过参数 -l 自定义长度,能一行显示完成的代码会放在一行,比如有多个元素的列表.后者把多个元素放在一行,显然更易读,而且代码更紧凑(如果你的工资是按照代码行数来算的话,不建议这么做),Black 是 PEP8 的严格子集。
设置每行代码最大长度
black -l 150 test.py
对当前目录及子目录中的 多个py文件格式化
black `find . -name "*.py"`
今天关于如何在python注释中格式化长网址,并且仍符合PEP8的分享就到这里,希望大家有所收获,若想了解更多关于Pycharm 配置 autopep8 让 Python 代码更符合 pep8 规范、Pycharm配置autopep8教程,让Python代码更符合pep8规范、python – 如何在PyCharm中格式化多行TODO注释?、python3 格式化代码工具(autopep8 black)等相关知识,可以在本站进行查询。
本文标签: