python Autopep8实现按PEP8风格自动排版Python代码
Autopep8是一个将Python代码自动排版为PEP8风格的小工具。它使用pep8工具来决定代码中的哪部分需要被排版。Autopep8可以修复大部分pep8工具中报告的排版问题。
参考网址:
https://www.python.org/dev/peps/pep-0008/
https://pypi.python.org/pypi/autopep8/
(1)安装步骤如下:
localhost:~a6$sudopipinstallautopep8 Password: Thedirectory'/Users/a6/Library/Caches/pip/http'oritsparentdirectoryisnotownedbythecurrentuserandthecachehasbeendisabled.Pleasecheckthepermissionsandownerofthatdirectory.Ifexecutingpipwithsudo,youmaywantsudo's-Hflag. Thedirectory'/Users/a6/Library/Caches/pip'oritsparentdirectoryisnotownedbythecurrentuserandcachingwheelshasbeendisabled.checkthepermissionsandownerofthatdirectory.Ifexecutingpipwithsudo,youmaywantsudo's-Hflag. Collectingautopep8 Collectingpycodestyle>=2.3(fromautopep8) Downloadingpycodestyle-2.3.1-py2.py3-none-any.whl(45kB) 100%|████████████████████████████████|51kB324kB/s Installingcollectedpackages:pycodestyle,autopep8 Successfullyinstalledautopep8-1.3.3pycodestyle-2.3.1 localhost:~a6$sudopipinstallautopep8 Thedirectory'/Users/a6/Library/Caches/pip/http'oritsparentdirectoryisnotownedbythecurrentuserandthecachehasbeendisabled.Pleasecheckthepermissionsandownerofthatdirectory.Ifexecutingpipwithsudo,youmaywantsudo's-Hflag. Thedirectory'/Users/a6/Library/Caches/pip'oritsparentdirectoryisnotownedbythecurrentuserandcachingwheelshasbeendisabled.checkthepermissionsandownerofthatdirectory.Ifexecutingpipwithsudo,youmaywantsudo's-Hflag. Requirementalreadysatisfied:autopep8in/Library/Python/2.7/site-packages Requirementalreadysatisfied:pycodestyle>=2.3in/Library/Python/2.7/site-packages(fromautopep8)
(2)示例代码:
1)运行命令前代码的排版(保存在test_autopep8.py)
importmath,sys; defexample1(): ####Thisisalongcomment.Thisshouldbewrappedtofitwithin72characters. some_tuple=(1,2,3,'a'); some_variable={'long':'Longcodelinesshouldbewrappedwithin79characters.', 'other':[math.pi,100,200,300,9876543210,'Thisisalongstringthatgoeson'], 'more':{'inner':'Thiswholelogicallineshouldbewrapped.',some_tuple:[1, 20,300,40000,500000000,60000000000000000]}} return(some_tuple,some_variable) defexample2():return{'has_key()isdeprecated':True}.has_key({'f':2}.has_key('')); classExample3(object): def__init__(self,bar): #Commentsshouldhaveaspaceafterthehash. ifbar:bar+=1;bar=bar*bar;returnbar else: some_string=""" Indentationinmultilinestringsshouldnotbetouched. Onlyactualcodeshouldbereindented. """ return(sys.path,some_string)
2)运行命令
bogon:ABa6$autopep8--in-place--aggressive--aggressivetest_autopep8.py
3)运行命令后代码的排版
importmath importsys defexample1(): #Thisisalongcomment.Thisshouldbewrappedtofitwithin72 #characters. some_tuple=(1,2,3,'a') some_variable={ 'long':'Longcodelinesshouldbewrappedwithin79characters.', 'other':[ math.pi, 100, 200, 300, 9876543210, 'Thisisalongstringthatgoeson'], 'more':{ 'inner':'Thiswholelogicallineshouldbewrapped.', some_tuple:[ 1, 20, 300, 40000, 500000000, 60000000000000000]}} return(some_tuple,some_variable) defexample2():return(''in{'f':2})in{'has_key()isdeprecated':True}; classExample3(object): def__init__(self,bar): #Commentsshouldhaveaspaceafterthehash. ifbar: bar+=1 bar=bar*bar returnbar else: some_string=""" Indentationinmultilinestringsshouldnotbetouched. Onlyactualcodeshouldbereindented. """ return(sys.path,some_string)
4)参考网址:
https://github.com/hhatto/autopep8
到此这篇关于pythonAutopep8实现按PEP8风格自动排版Python代码的文章就介绍到这了,更多相关pythonAutopep8自动排版内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。