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 ORM框架SQLAlchemy学习笔记之关系映射实例
Jun 10 Python
Python入门及进阶笔记 Python 内置函数小结
Aug 09 Python
使用Python下的XSLT API进行web开发的简单教程
Apr 15 Python
Python基于Tkinter实现的记事本实例
Jun 17 Python
python 读写中文json的实例详解
Oct 29 Python
python基于ID3思想的决策树
Jan 03 Python
python执行CMD指令,并获取返回的方法
Dec 19 Python
解决Pycharm调用Turtle时 窗口一闪而过的问题
Feb 16 Python
Python中的random.uniform()函数教程与实例解析
Mar 02 Python
python利用dlib获取人脸的68个landmark
Nov 27 Python
解决os.path.isdir() 判断文件夹却返回false的问题
Nov 29 Python
python3 webp转gif格式的实现示例
Dec 10 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定时删除文件夹下文件(清理缓存文件)
2013/01/23 PHP
php对大文件进行读取操作的实现代码
2013/01/23 PHP
php类中的各种拦截器用法分析
2014/11/03 PHP
php+xml结合Ajax实现点赞功能完整实例
2015/01/30 PHP
php通过排列组合实现1到9数字相加都等于20的方法
2015/08/03 PHP
PHP 数组基本操作方法详解
2016/06/17 PHP
解决Laravel 不能创建 migration 的问题
2019/10/09 PHP
Ajax::prototype 源码解读
2007/01/22 Javascript
javascript 控制弹出窗口
2007/04/10 Javascript
jQuery点击后一组图片左右滑动的实现代码
2012/08/16 Javascript
使用node.js半年来总结的 10 条经验
2014/08/18 Javascript
JS实现文字链接感应鼠标淡入淡出改变颜色的方法
2015/02/26 Javascript
jquery实现浮动在网页右下角的彩票开奖公告窗口代码
2015/09/04 Javascript
JS实现带鼠标效果的头像及文章列表代码
2015/09/27 Javascript
javascript图片预加载完整实例
2015/12/10 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
用NODE.JS中的流编写工具是要注意的事项
2016/03/01 Javascript
从零开始学习Node.js系列教程五:服务器监听方法示例
2017/04/13 Javascript
JavaScript解析任意形式的json树型结构展示
2017/07/23 Javascript
清空元素html("") innerHTML="" 与 empty()的区别和应用(推荐)
2017/08/14 Javascript
arctext.js实现文字平滑弯曲弧形效果的插件
2019/05/13 Javascript
python中Genarator函数用法分析
2015/04/08 Python
Python实现字典的key和values的交换
2015/08/04 Python
Python中表达式x += y和x = x+y 的区别详解
2017/06/20 Python
关于python之字典的嵌套,递归调用方法
2019/01/21 Python
如何通过Python3和ssl实现加密通信功能
2020/05/09 Python
在pycharm中debug 实时查看数据操作(交互式)
2020/06/09 Python
Python 带星号(* 或 **)的函数参数详解
2021/02/23 Python
html5 canvas简单封装一个echarts实现不了的饼图
2018/06/12 HTML / CSS
高中自我评价分享
2013/12/05 职场文书
读群众路线心得体会
2014/03/07 职场文书
租房协议书
2014/09/12 职场文书
2014年敬老院工作总结
2014/12/08 职场文书
留学推荐信(中英文版)
2015/03/26 职场文书
复兴之路观后感3000字
2015/06/02 职场文书
八年级物理教学反思
2016/02/19 职场文书