zoukankan      html  css  js  c++  java
  • 杂谈——Python代码写得丑怎么办?autopep8来帮你

    欢迎关注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 了。

  • 相关阅读:
    Django-website 程序案例系列-3 URL详解
    Django-website 程序案例系列-1 最简单的web服务器
    c# 多维数组、交错数组(转化为DataTable)
    c++(重载等号=操作为深拷贝)
    c# 导入c++ dll
    Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用
    spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)
    hibernate mapping文件中 xmlns会导致linq to xml 查询不到对应的节点
    linq to xml
    xml 操作(动态添加 property属性 其他节点同理)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302776.html
Copyright © 2011-2022 走看看