python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼
Autopep8是一個(gè)將Python代碼自動(dòng)排版為PEP8風(fēng)格的小工具。它使用pep8工具來決定代碼中的哪部分需要被排版。Autopep8可以修復(fù)大部分pep8工具中報(bào)告的排版問題。
參考網(wǎng)址:
https://www.python.org/dev/peps/pep-0008/
https://pypi.python.org/pypi/autopep8/
(1)安裝步驟如下:
localhost:~ a6$ sudo pip install autopep8 Password: The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting autopep8 Collecting pycodestyle>=2.3 (from autopep8) Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB) 100% |████████████████████████████████| 51kB 324kB/s Installing collected packages: pycodestyle, autopep8 Successfully installed autopep8-1.3.3 pycodestyle-2.3.1 localhost:~ a6$ sudo pip install autopep8 The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Requirement already satisfied: autopep8 in /Library/Python/2.7/site-packages Requirement already satisfied: pycodestyle>=2.3 in /Library/Python/2.7/site-packages (from autopep8)
(2)示例代碼:
1)運(yùn)行命令前代碼的排版 (保存在test_autopep8.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,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)
2)運(yùn)行命令
bogon:AB a6$ autopep8 --in-place --aggressive --aggressive test_autopep8.py
3)運(yùn)行命令后代碼的排版
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)
4)參考網(wǎng)址:
https://github.com/hhatto/autopep8
到此這篇關(guān)于python Autopep8實(shí)現(xiàn)按PEP8風(fēng)格自動(dòng)排版Python代碼的文章就介紹到這了,更多相關(guān)python Autopep8自動(dòng)排版內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?eval()和exec()函數(shù)使用詳解
exec函數(shù)執(zhí)行的是python語句,沒有返回值,eval函數(shù)執(zhí)行的是python表達(dá)式,有返回值,exec函數(shù)和eval函數(shù)都可以傳入命名空間作為參數(shù),本文給大家介紹下Python?eval()和exec()函數(shù),感興趣的朋友跟隨小編一起看看吧2022-11-11
Python對(duì)字符串實(shí)現(xiàn)去重操作的方法示例
字符串去重是python中字符串操作常見的一個(gè)需求,最近在工作中就又遇到了,所以下面這篇文章主要給大家介紹了關(guān)于Python對(duì)字符串實(shí)現(xiàn)去重操作的相關(guān)資料,文中給出了詳細(xì)的介紹,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
python PyQt5 爬蟲實(shí)現(xiàn)代碼
這篇文章主要介紹了python PyQt5 爬蟲實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù)
對(duì)于數(shù)據(jù)量大的情況(比如用戶之間有邀請(qǐng)鏈,有點(diǎn)三級(jí)分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進(jìn)行存儲(chǔ)。這篇文章主要介紹了Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù),需要的朋友可以參考下2019-06-06
利用Python統(tǒng)計(jì)Jira數(shù)據(jù)并可視化
目前公司使用 Jira 作為項(xiàng)目管理工具,在每一次迭代完成后的復(fù)盤會(huì)上,我們都需要針對(duì)本次迭代的 Bug 進(jìn)行數(shù)據(jù)統(tǒng)計(jì),以幫助管理層能更直觀的了解研發(fā)的代碼質(zhì)量。本篇文章將介紹如何利用統(tǒng)計(jì) Jira 數(shù)據(jù),并進(jìn)行可視化,需要的可以參考一下2022-07-07
python實(shí)現(xiàn)定時(shí)播放mp3
這篇文章主要介紹了python實(shí)現(xiàn)定時(shí)播放mp3,程序非常簡(jiǎn)單,功能很實(shí)用,主要是使用python實(shí)現(xiàn)了一首mp3歌每半小時(shí)播放一次,有需要的小伙伴可以參考下。2015-03-03

