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 相关文章推荐
合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友
Apr 09 Python
Python 的描述符 descriptor详解
Feb 27 Python
非递归的输出1-N的全排列实例(推荐)
Apr 11 Python
浅析Python3中的对象垃圾收集机制
Jun 06 Python
PyQt5多线程刷新界面防假死示例
Dec 13 Python
关于Tensorflow 模型持久化详解
Feb 12 Python
Python socket连接中的粘包、精确传输问题实例分析
Mar 24 Python
Django框架配置mysql数据库实现过程
Apr 22 Python
python3+selenium获取页面加载的所有静态资源文件链接操作
May 04 Python
简单介绍一下pyinstaller打包以及安全性的实现
Jun 02 Python
python实现快速文件格式批量转换的方法
Oct 16 Python
Python中Matplotlib的点、线形状、颜色以及绘制散点图
Apr 07 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类中private属性继承问题分析
2012/11/01 PHP
完美解决PHP中的Cannot modify header information 问题
2013/08/12 PHP
2014年最新推荐的10款 PHP 开发框架
2014/08/01 PHP
php生成word并下载代码实例
2019/03/15 PHP
PHP设计模式之策略模式原理与用法实例分析
2019/04/04 PHP
JavaScript 小型打飞机游戏实现原理说明
2010/10/28 Javascript
使用jQuery动态加载js脚本文件的方法
2014/04/03 Javascript
javascript 获取浏览器版本
2015/01/21 Javascript
PHP+jQuery+Ajax+Mysql如何实现发表心情功能
2015/08/06 Javascript
快速掌握WordPress中加载JavaScript脚本的方法
2015/12/17 Javascript
iscroll.js的上拉下拉刷新时无法回弹的解决方法
2016/02/18 Javascript
再谈Javascript中的基本类型和引用类型(推荐)
2016/07/01 Javascript
使用node.js搭建服务器
2017/05/20 Javascript
使用JS和canvas实现gif动图的停止和播放代码
2017/09/01 Javascript
Vue集成Iframe页面的方法示例
2017/12/12 Javascript
12条写出高质量JS代码的方法
2018/01/07 Javascript
js和jQuery以及easyui实现对下拉框的指定赋值方法
2018/01/23 jQuery
[44:21]Ti4 循环赛第四日 附加赛NEWBEE vs LGD
2014/07/13 DOTA
Python 查找list中的某个元素的所有的下标方法
2018/06/27 Python
如何利用Boost.Python实现Python C/C++混合编程详解
2018/11/08 Python
Python lxml模块的基本使用方法分析
2019/12/21 Python
台湾深度自由行旅游平台:Tripbaa趣吧
2017/10/10 全球购物
中东最大的在线宠物店:Dubai Pet Food
2020/06/11 全球购物
英国健身专家:WIT Fitness
2021/02/09 全球购物
个人简历中的自我评价范例
2013/10/29 职场文书
电气工程及自动化专业自荐书范文
2013/12/18 职场文书
高中语文教学反思
2014/01/16 职场文书
社区春季防火方案
2014/06/02 职场文书
企业宣传工作方案
2014/06/02 职场文书
乡镇干部个人对照检查材料思想汇报(原创篇)
2014/09/28 职场文书
幼儿园教师个人总结
2015/02/05 职场文书
2015年财务部年度工作总结
2015/05/19 职场文书
Python基于百度AI实现抓取表情包
2021/06/27 Python
JavaScript的function函数详细介绍
2021/11/20 Javascript
HTML5基础学习之文本标签控制
2022/03/25 HTML / CSS
springboot读取nacos配置文件
2022/05/20 Java/Android