按PEP8风格自动排版Python代码

时间:2021-08-22 07:35:11

如果用编辑器写代码,很容易会怎么顺手怎么来,而每个人的写代码习惯有很大不同,这样会导致可读性不是很好,代码维护起来比较困难,当然各种编辑器一般都有各种用来自动排版的插件,例如Sublime Text就有:Python PEP8 Autoformat,使用方法:

默认快捷键:

Ctrl + Shift + R



不过,python的一些install脚本还是很强大的,例如python的autopep8库:
(链接:https://github.com/hhatto/autopep8

$ pip install --upgrade autopep8



然后在命令行:

$ autopep8 --in-place --aggressive --aggressive <filename>



就可以对代码进行pep8自动排版,对比了下效果,如下:


排版前

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)



使用Sublime Text中的Python PEP8 Autoformat自动排版

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 {'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)



使用autopep8自动排版

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)



可以看到虽然都是PEP8风格,但是效果还是略有不同,可以根据需要个人习惯进行有选择的使用。