目錄
- 官網(wǎng)
- 前言
- 安裝
- 配置
- 展示
官網(wǎng)
autopep8 · PyPI——https://pypi.org/project/autopep8/
前言
Python 編程語言需要遵循
PEP8
規(guī)范,但是很多人在編寫代碼時(shí)往往記不住這個(gè)規(guī)范,代碼寫得比較丑。這怎么辦呢?別擔(dān)心,autopep8 來幫你。
autopep8 可以自動(dòng)格式化 Python 代碼以符合
PEP8
規(guī)范。它使用
pycodestyle
實(shí)用程序來確定需要格式化代碼的是哪些部分。autopep8 能夠修復(fù)
pycodestyle
可以報(bào)告的大多數(shù)格式問題。只需要使用 autopep8,麻麻再也不用擔(dān)心我的代碼不規(guī)范了。
安裝
使用
pip
進(jìn)行安裝即可,不會安裝
pip
的可以看一下這個(gè)博客——python中pip安裝、升級、升級指定的包。安裝
pip
之后,運(yùn)行
cmd
命令窗。
使用如下命令安裝 autopep8 即可。
pip install autopep8
配置
文件(File)- 設(shè)置(Settings)- 工具(Tools)- 外部工具(External Tools)- 添加(+)
然后會出現(xiàn)這個(gè)界面,下面就可以設(shè)置配置參數(shù)了。
Name:
autopep8
Program:
autopep8
Arguments:
--in-place --aggressive --aggressive $FilePath$
Working directory:
$ProjectFileDir$
Output filters:
$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
把上面的配置參數(shù)按照對應(yīng)的名字填寫就可以了,具體配置如下圖:
然后點(diǎn)擊 OK - Apply - OK 即可。
到這里就已經(jīng)設(shè)置完畢了,使用方法也比較簡單,將鼠標(biāo)在文件編輯器中 - 點(diǎn)擊右鍵 - External Tools - autopep8,這樣就會自動(dòng)運(yùn)行 autopep8,幫你規(guī)范化代碼。
展示
使用 autopep8 前:
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
,
300
,
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
)
import
math
import
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
,
300
,
40000
,
500000000
,
60000000000000000
]
}
}
return
(
some_tuple
,
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
)
是不是比原來更優(yōu)美了呢?哈哈哈,到這里就 OK 了。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
