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

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