autopep8是一个将python代码自动排版为pep8风格的小工具。它使用pep8工具来决定代码中的哪部分需要被排版。autopep8可以修复大部分pep8工具中报告的排版问题。
参考网址:
https://www.python.org/dev/peps/pep-0008/
https://pypi.python.org/pypi/autopep8/
(1)安装步骤如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)运行命令前代码的排版 (保存在test_autopep8.py)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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)运行命令
1
|
bogon:ab a6$ autopep8 - - in - place - - aggressive - - aggressive test_autopep8.py |
3)运行命令后代码的排版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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)参考网址:
https://github.com/hhatto/autopep8
到此这篇关于python autopep8实现按pep8风格自动排版python代码的文章就介绍到这了,更多相关python autopep8自动排版内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/helloxiaozhe/article/details/78954381