Autopep8的使用

Autopep8的使用

什么是Autopep8

在python开发中, 大家都知道,python编码规范是PEP8,但是在市级开发中有的公司严格要求PEP8规范开发, 有的公司不会在乎那些,在我的理解中,程序员如果想走的更高,或者更远,干任何事情必须得专业化(本人理解方式), 不要求很多东西都是精通,但最少得有一门精通的语言,小弟在此在大佬面前装逼了, 忘看过的大牛不要揭穿, 留下你懂的我不懂的知识,大家一起学习,一起进步。 谢谢。

Autopep8是一个将python代码自动编排的一个工具,它使用pep8工具来决定代码中的那部分需要被排版,Autopep8可以修复大部分pep8工具中报告的排版问题。很多人都知道 Ctrl+Alt+L 也可以排版, 但是我要告诉你,快捷键只是可以简单的排版。跟Autopep8是无法相比的。

安装Autopep8:

pip install autopep8

安装完成之后,import导入一下,测试是否安装成功。

Aytopep8的使用

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

复制代码

def example1():
    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
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""

复制代码

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

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

然后输入以下命令:

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

file.py 是你的demo.py

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

复制代码

import math
import sys


def example1():
    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
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.Only actual code should be reindented.
"""

复制代码

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

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

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

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

img

复制代码

Name: 名称可以随意

Program: autopep8        # 前提必须先安装
Arguments: --in-place --aggressive --aggressive $FilePath$
Working directory: $ProjectFileDir$

Advanced Options
                ---- Outputfilters:
$FILE_PATH$\:$LINE$\:$COLUMN$\:.*

复制代码

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

快捷使用:

img

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

转载于:https://www.cnblogs.com/wenqiangit/p/10901749.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值