Autopep8的使用(python自动编排工具)


Posted in Python onMarch 02, 2021

什么是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

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

Autopep8的使用(python自动编排工具)

Name: 名称可以随意

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

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

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

快捷使用:

Autopep8的使用(python自动编排工具)

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

Autopep8的使用(python自动编排工具)

到此这篇关于Autopep8的使用(python自动编排工具)的文章就介绍到这了,更多相关Autopep8 使用内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python判断字符串是否包含子字符串的方法
Mar 24 Python
Python实现把xml或xsl转换为html格式
Apr 08 Python
python定时器(Timer)用法简单实例
Jun 04 Python
Python编程实现线性回归和批量梯度下降法代码实例
Jan 04 Python
浅谈关于Python3中venv虚拟环境
Aug 01 Python
在python plt图表中文字大小调节的方法
Jul 08 Python
Python列表元素常见操作简单示例
Oct 25 Python
python实现输入任意一个大写字母生成金字塔的示例
Oct 27 Python
Python+Django+MySQL实现基于Web版的增删改查的示例代码
May 13 Python
Python函数参数定义及传递方式解析
Jun 10 Python
selenium切换标签页解决get超时问题的完整代码
Aug 30 Python
python制作图形界面的2048游戏, 基于tkinter
Apr 06 Python
python 将Excel转Word的示例
Mar 02 #Python
Python字节单位转换(将字节转换为K M G T)
Mar 02 #Python
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
Mar 02 #Python
jupyter notebook指定启动目录的方法
Mar 02 #Python
python实现发送邮件
Mar 02 #Python
matplotlib阶梯图的实现(step())
Mar 02 #Python
Python读写Excel表格的方法
Mar 02 #Python
You might like
深入php处理整数函数的详解
2013/06/09 PHP
php封装的验证码工具类完整实例
2016/10/19 PHP
php使用环形链表解决约瑟夫问题完整示例
2018/08/07 PHP
PHP常量及变量区别原理详解
2020/08/14 PHP
jQuery学习笔记之DOM对象和jQuery对象
2010/12/22 Javascript
JS禁用浏览器退格键实现思路及代码
2013/10/29 Javascript
jQuery遍历Table应用示例
2014/04/09 Javascript
Query常用DIV操作获取和设置长度宽度的实现方法
2016/09/19 Javascript
JavaScript组件开发之输入框加候选框
2017/03/10 Javascript
基于Bootstrap的标签页组件及bootstrap-tab使用说明
2017/07/25 Javascript
mui back 返回刷新页面的实例
2017/12/06 Javascript
深入浅析Vue.js计算属性和侦听器
2018/05/05 Javascript
Vue匿名插槽与作用域插槽的合并和覆盖行为
2019/04/22 Javascript
如何自定义微信小程序tabbar上边框的颜色
2019/07/09 Javascript
Jquery动态列功能完整实例
2019/08/30 jQuery
vue过滤器实现日期格式化的案例分析
2020/07/02 Javascript
完美解决通过IP地址访问VUE项目的问题
2020/07/18 Javascript
python映射列表实例分析
2015/01/26 Python
Python中字符串的格式化方法小结
2016/05/03 Python
详解使用pymysql在python中对mysql的增删改查操作(综合)
2017/01/18 Python
python爬虫面试宝典(常见问题)
2018/03/02 Python
Python多进程写入同一文件的方法
2019/01/14 Python
Python内存管理实例分析
2019/07/10 Python
Django 实现外键去除自动添加的后缀‘_id’
2019/11/15 Python
Python特殊属性property原理及使用方法解析
2020/10/09 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
2021/01/06 Python
利用CSS3的flexbox实现水平垂直居中与三列等高布局
2016/09/12 HTML / CSS
css3 transform及原生js实现鼠标拖动3D立方体旋转
2016/06/20 HTML / CSS
企业车辆管理制度
2014/01/24 职场文书
党员干部公开承诺书
2014/03/26 职场文书
毕业生欢送会主持词
2014/03/31 职场文书
仓库统计员岗位职责
2015/04/14 职场文书
2015年音乐教师个人工作总结
2015/05/20 职场文书
2015年网络管理员工作总结
2015/05/21 职场文书
2015年房地产个人工作总结
2015/05/26 职场文书
Windows10下安装MySQL8
2021/04/06 MySQL