python Autopep8实现按PEP8风格自动排版Python代码


Posted in Python onMarch 02, 2021

Autopep8是一个将Python代码自动排版为PEP8风格的小工具。它使用pep8工具来决定代码中的哪部分需要被排版。Autopep8可以修复大部分pep8工具中报告的排版问题。

参考网址:

https://www.python.org/dev/peps/pep-0008/

https://pypi.python.org/pypi/autopep8/

(1)安装步骤如下:

localhost:~ a6$ sudo pip install autopep8
Password:
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting autopep8
Collecting pycodestyle>=2.3 (from autopep8)
 Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB)
  100% |????????????????????????????????| 51kB 324kB/s
Installing collected packages: pycodestyle, autopep8
Successfully installed autopep8-1.3.3 pycodestyle-2.3.1
localhost:~ a6$ sudo pip install autopep8
The directory '/Users/a6/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/a6/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: autopep8 in /Library/Python/2.7/site-packages
Requirement already satisfied: pycodestyle>=2.3 in /Library/Python/2.7/site-packages (from autopep8)

(2)示例代码:

1)运行命令前代码的排版 (保存在test_autopep8.py)

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)

2)运行命令

bogon:AB a6$ autopep8 --in-place --aggressive --aggressive test_autopep8.py

3)运行命令后代码的排版

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)

4)参考网址:
https://github.com/hhatto/autopep8

到此这篇关于python Autopep8实现按PEP8风格自动排版Python代码的文章就介绍到这了,更多相关python Autopep8自动排版内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
用Python写的图片蜘蛛人代码
Aug 27 Python
在Python中使用poplib模块收取邮件的教程
Apr 29 Python
python自定义类并使用的方法
May 07 Python
深入解析Python编程中super关键字的用法
Jun 24 Python
视觉直观感受若干常用排序算法
Apr 13 Python
python opencv人脸检测提取及保存方法
Aug 03 Python
python利用插值法对折线进行平滑曲线处理
Dec 25 Python
Django model select的多种用法详解
Jul 16 Python
Pycharm 文件更改目录后,执行路径未更新的解决方法
Jul 19 Python
对Python中一维向量和一维向量转置相乘的方法详解
Aug 26 Python
python常见字符串处理函数与用法汇总
Oct 30 Python
python模式 工厂模式原理及实例详解
Feb 11 Python
pycharm配置安装autopep8自动规范代码的实现
Mar 02 #Python
Python实现我的世界小游戏源代码
Mar 02 #Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
Mar 02 #Python
python 基于pygame实现俄罗斯方块
Mar 02 #Python
使用Python快速打开一个百万行级别的超大Excel文件的方法
Mar 02 #Python
Autopep8的使用(python自动编排工具)
Mar 02 #Python
python 将Excel转Word的示例
Mar 02 #Python
You might like
php 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)
2010/05/16 PHP
PHP Session_Regenerate_ID函数双释放内存破坏漏洞
2011/01/27 PHP
PHP实现获取文件后缀名的几种常用方法
2015/08/08 PHP
PHP curl模拟登录带验证码的网站
2015/11/30 PHP
Yii框架数据模型的验证规则rules()被执行的方法
2016/12/02 PHP
javascript 二维数组的实现与应用
2010/03/16 Javascript
js下获得客户端操作系统的函数代码(1:vista,2:windows7,3:2000,4:xp,5:2003,6:2008)
2011/10/31 Javascript
javascript 事件处理、鼠标拖动效果实现方法详解
2012/05/11 Javascript
Android中资源文件(非代码部分)的使用概览
2012/12/18 Javascript
JS删除数组元素的函数介绍
2013/03/27 Javascript
js关闭浏览器窗口及检查浏览器关闭事件
2013/09/03 Javascript
javascript进行四舍五入方法汇总
2014/12/16 Javascript
jQuery中用dom操作替代正则表达式
2014/12/29 Javascript
javascript获取当前鼠标坐标的方法
2015/01/10 Javascript
javascript实现图片延迟加载方法汇总(三种方法)
2015/08/27 Javascript
详解Javascript中prototype属性(推荐)
2016/09/03 Javascript
探讨AngularJs中ui.route的简单应用
2016/11/16 Javascript
用VueJS写一个Chrome浏览器插件的实现方法
2019/02/27 Javascript
vue+iview/elementUi实现城市多选
2019/03/28 Javascript
JavaScript模块管理的简单实现方式详解
2019/06/15 Javascript
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
Python中is和==的区别详解
2018/11/15 Python
python pytest进阶之conftest.py详解
2019/06/27 Python
如何安装2019Pycharm最新版本(详细教程)
2019/09/26 Python
TFRecord格式存储数据与队列读取实例
2020/01/21 Python
使用Tkinter制作信息提示框
2020/02/18 Python
html5菜单折纸效果
2014/04/22 HTML / CSS
巴西食品补充剂在线零售商:Músculos na Web
2017/08/07 全球购物
大学生四个方面的自我评价
2013/09/19 职场文书
高一地理教学反思
2014/01/18 职场文书
开学典礼决心书
2014/03/11 职场文书
民生工作实施方案
2014/05/31 职场文书
学雷锋宣传标语
2014/06/25 职场文书
2015年社区纪检工作总结
2015/04/21 职场文书
驳回起诉民事裁定书
2015/05/19 职场文书
宾馆卫生管理制度
2015/08/06 职场文书