杂谈——Python代码写得丑怎么办?autopep8来帮你

    xiaoxiao2025-05-17  3

    欢迎关注WX公众号:【程序员管小亮】

    目录

    欢迎关注WX公众号:【程序员管小亮】官网前言安装配置展示

    官网

    autopep8 · PyPI——https://pypi.org/project/autopep8/

    前言

    Python 编程语言需要遵循 PEP8 规范,但是很多人在编写代码时往往记不住这个规范,代码写得比较丑。这怎么办呢?别担心,autopep8 来帮你。

    autopep8 可以自动格式化 Python 代码以符合 PEP8 规范。它使用 pycodestyle 实用程序来确定需要格式化代码的是哪些部分。autopep8 能够修复 pycodestyle 可以报告的大多数格式问题。只需要使用 autopep8,麻麻再也不用担心我的代码不规范了。

    安装

    使用 pip 进行安装即可,不会安装 pip 的可以看一下这个博客——python中pip安装、升级、升级指定的包。安装 pip 之后,运行 cmd 命令窗。 使用如下命令安装 autopep8 即可。

    pip install autopep8

    配置

    文件(File)- 设置(Settings)- 工具(Tools)- 外部工具(External Tools)- 添加(+) 然后会出现这个界面,下面就可以设置配置参数了。 Name:

    autopep8

    Program:

    autopep8

    Arguments:

    --in-place --aggressive --aggressive $FilePath$

    Working directory:

    $ProjectFileDir$

    Output filters:

    $FILE_PATH$\:$LINE$\:$COLUMN$\:.*

    把上面的配置参数按照对应的名字填写就可以了,具体配置如下图: 然后点击 OK - Apply - OK 即可。 到这里就已经设置完毕了,使用方法也比较简单,将鼠标在文件编辑器中 - 点击右键 - External Tools - autopep8,这样就会自动运行 autopep8,帮你规范化代码。

    展示

    使用 autopep8 前:

    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)

    使用 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)

    是不是比原来更优美了呢?哈哈哈,到这里就 OK 了。

    我是管小亮 认证博客专家 TensorFlow PyTorch 图像处理 纸上得来终觉浅,绝知此事要躬行包括但不局限于机器学习,深度学习,自然语言处理,计算机视觉,知识图谱,还有Python,C++,TensorFlow,Pytorch,Keras,PaddlePaddle等,不定时分享资源(电子书/项目/工具),学习路线,思考,面经等!!!
    最新回复(0)