签到成功

知道了

CNDBA社区CNDBA社区

Python自动格式化代码(PEP8格式)

2018-11-26 16:09 2268 0 原创 python开发
作者: Expect-乐

1.关于pep8格式

https://www.python.org/dev/peps/pep-0008/http://www.cndba.cn/Expect-le/article/3176

http://www.cndba.cn/Expect-le/article/3176

Github地址 :https://github.com/hhatto/autopep8

2.格式化代码

通过第三方python代码(autopep8)来格式化代码。http://www.cndba.cn/Expect-le/article/3176

http://www.cndba.cn/Expect-le/article/3176

2.1安装

pip install --upgrade autopep8

2.2使用

原python代码格式

http://www.cndba.cn/Expect-le/article/3176

# cat test.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)

格式化后的代码:
将格式化后的代码重新写入文件中http://www.cndba.cn/Expect-le/article/3176http://www.cndba.cn/Expect-le/article/3176

# autopep8 --in-place --aggressive --aggressive test.py

或直接看格式化效果,不覆盖原有的代码http://www.cndba.cn/Expect-le/article/3176

autopep8 --aggressive --aggressive test.py

查看格式化后代码:http://www.cndba.cn/Expect-le/article/3176http://www.cndba.cn/Expect-le/article/3176

# cat test.py
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

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
Expect-乐

Expect-乐

关注

Without the continuous bitter cold, there can be no fragrant plum blossom

  • 336
    原创
  • 6
    翻译
  • 100
    转载
  • 41
    评论
  • 访问:1374330次
  • 积分:1957
  • 等级:核心会员
  • 排名:第3名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ