• 代码规范_autopep8代码格式化工具


    一、什么是Autopep8


    Autopep8是一个将python代码自动编排的一个工具,它使用pep8工具来决定代码中的那部分需要被排版,Autopep8可以修复大部分pep8工具中报告的排版问题。

    很多人都知道  Ctrl+Alt+L 也可以排版, 但是我要告诉你,快捷键只是可以简单的排版。

    跟Autopep8是无法相比的。 

    安装Autopep8:

    pip install autopep8

    二、Autopep8的使用

    安装完成之后,打开pycharm,创建一个新的python文件,  demo.py 将一下代码放入文件中。

     1 def example1():
     2     some_tuple = (1, 2, 3, 'a')
     3     some_variable = {
     4         'long': 'Long code lines should be wrapped within 79 characters.',
     5         'other': [math.pi, 100, 200, 300, 9876543210,'This is a long string that goes on'],
     6         'more': { 'inner': 'This whole logical line should be wrapped.',some_tuple: [ 1,20, 300, 40000,500000000,60000000000000000]}}
     7     return (some_tuple, some_variable)
     8 
     9 def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True};
    10 
    11 class Example3(object):
    12     def __init__(self, bar):
    13         # Comments should have a space after the hash.
    14         if bar:
    15             bar += 1
    16             bar = bar * bar
    17         else:
    18             some_string = """
    19                        Indentation in multiline strings should not be touched.Only actual code should be reindented.
    20 """
    demo.py

    这几行代码看上去是不是很乱,   接下来就要使用:Autopep8模块了

    打开cmd找到demo.py的文件的上级目录,

    然后输入以下命令:

    autopep8 --in-place --aggressive --aggressive file.py

    file.py   是你的demo.py

    输入命令,按回车执行成功是不返回的, 执行完成之后就可以了,在次打开文件就可以看到变化了。

     1 import math
     2 
     3 
     4 def example1():
     5     some_tuple = (1, 2, 3, 'a')
     6     some_variable = {
     7         'long': 'Long code lines should be wrapped within 79 characters.',
     8         'other': [
     9             math.pi,
    10             100,
    11             200,
    12             300,
    13             9876543210,
    14             'This is a long string that goes on'],
    15         'more': {
    16             'inner': 'This whole logical line should be wrapped.',
    17             some_tuple: [
    18                 1,
    19                 20,
    20                 300,
    21                 40000,
    22                 500000000,
    23                 60000000000000000]}}
    24     return some_tuple, some_variable
    25 
    26 
    27 def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}
    28 
    29 
    30 class Example3(object):
    31     def __init__(self, bar):
    32         # Comments should have a space after the hash.
    33         if bar:
    34             bar += 1
    35             bar = bar * bar
    36         else:
    37             some_string = """
    38                        Indentation in multiline strings should not be touched.Only actual code should be reindented.
    39 """
    demo_update.py

    三、在Pycharm中配置使用

    执行完Autopep8之后代码是不是看上去简洁多了。

    有人会说,没写一个函数就执行一遍命令, 是不是有点麻烦啊, 是的, 有有点麻烦, 但是pycharm是可以配置的, 配置过程如下:

    1: File --->  Settings  --->  Tools  --->   External Tools

    打开之后,可以看见窗体左上角有一个 + 号, 点击+号添加。

    Name: 名称可以随意
    
    Program: autopep8        # 前提必须先安装
    Arguments: --in-place --aggressive --aggressive $FilePath$
    Working directory: $ProjectFileDir$
    
    Advanced Options
    Outputfilters:
    $FILE_PATH$:$LINE$:$COLUMN$:.*

    以上配置完成之后点击  OK 保存即可。

    快捷使用:

    Tools    --->    External Tools  --->   Autopep8       鼠标点击一下即可。

  • 相关阅读:
    在 Ubuntu上使用 MySQL
    Ubuntu/CentOs 搭建SVN服务器
    一个简单的零配置命令行HTTP服务器
    Windows 环境下 NodeJs 开发 Log
    AngularJS
    thinkphp5.0使用workerman多线程实例
    bat自动备份数据库文件
    mysql.ini配置优化速度参考
    php解析识别二维码内容
    windows屏蔽windows错误报告提示框
  • 原文地址:https://www.cnblogs.com/sunxiuwen/p/11162389.html
Copyright © 2020-2023  润新知