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 相关文章推荐
django 自定义用户user模型的三种方法
Nov 18 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
Nov 07 Python
浅谈Python实现2种文件复制的方法
Jan 19 Python
Python实现爬虫从网络上下载文档的实例代码
Jun 13 Python
Tensorflow 查看变量的值方法
Jun 14 Python
Python一键安装全部依赖包的方法
Aug 12 Python
python定位xpath 节点位置的方法
Aug 27 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
Aug 28 Python
python 动态调用函数实例解析
Oct 21 Python
Python操作Word批量生成合同的实现示例
Aug 28 Python
python 用Matplotlib作图中有多个Y轴
Nov 28 Python
Python plt 利用subplot 实现在一张画布同时画多张图
Feb 26 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
留言板翻页的实现详解
2006/10/09 PHP
WordPress中制作导航菜单的PHP核心方法讲解
2015/12/11 PHP
解决laravel中日志权限莫名变成了root的问题
2019/10/17 PHP
laravel使用数据库测试注意事项
2020/04/10 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
2020/08/17 PHP
对 jQuery 中 data 方法的误解分析
2014/06/18 Javascript
node.js中Socket.IO的进阶使用技巧
2014/11/04 Javascript
jQuery菜单插件用法实例
2015/07/25 Javascript
AngularJS实现的省市二级联动功能示例【可对选项实现增删】
2017/10/26 Javascript
在vue中使用express-mock搭建mock服务的方法
2018/11/07 Javascript
移动端滑动切换组件封装 vue-swiper-router实例详解
2018/11/25 Javascript
微信小程序 授权登录详解(附完整源码)
2019/08/23 Javascript
JS实现旋转木马轮播图
2020/01/01 Javascript
python动态性强类型用法实例
2015/05/09 Python
简单学习Python多进程Multiprocessing
2017/08/29 Python
python去除文件中重复的行实例
2018/06/29 Python
浅谈python下tiff图像的读取和保存方法
2018/12/04 Python
两个元祖T1=('a', 'b'),T2=('c', 'd')使用匿名函数将其转变成[{'a': 'c'},{'b': 'd'}]的几种方法
2019/03/05 Python
详解python路径拼接os.path.join()函数的用法
2019/10/09 Python
利用matplotlib实现根据实时数据动态更新图形
2019/12/13 Python
python numpy--数组的组合和分割实例
2020/02/24 Python
django实现HttpResponse返回json数据为中文
2020/03/27 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
解决python打开https出现certificate verify failed的问题
2020/09/03 Python
在HTML5中使用MathML数学公式的简单讲解
2016/02/19 HTML / CSS
好的自荐信包括什么内容
2013/11/07 职场文书
资料员岗位职责
2013/11/17 职场文书
企事业单位求职者的自我评价
2013/12/28 职场文书
公司道歉信范文
2014/01/09 职场文书
初婚初育证明
2014/01/14 职场文书
我们的节日端午节活动方案
2014/03/02 职场文书
法制报告会主持词
2014/04/02 职场文书
好听的队名和口号
2014/06/09 职场文书
幼儿园教师个人总结
2015/02/05 职场文书
2015年信息中心工作总结
2015/05/25 职场文书
SpringBoot工程下使用OpenFeign的坑及解决
2021/07/02 Java/Android