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 相关文章推荐
pycharm 使用心得(七)一些实用功能介绍
Jun 06 Python
初步讲解Python中的元组概念
May 21 Python
Python正则表达式使用经典实例
Jun 21 Python
Python 爬虫学习笔记之正则表达式
Sep 21 Python
浅析python中SQLAlchemy排序的一个坑
Feb 24 Python
python web.py开发httpserver解决跨域问题实例解析
Feb 12 Python
Python使用pymongo模块操作MongoDB的方法示例
Jul 20 Python
Python逐行读取文件中内容的简单方法
Feb 26 Python
python3.7将代码打包成exe程序并添加图标的方法
Oct 11 Python
Python协程操作之gevent(yield阻塞,greenlet),协程实现多任务(有规律的交替协作执行)用法详解
Oct 14 Python
Python数据可视化:顶级绘图库plotly详解
Dec 07 Python
利用python清除移动硬盘中的临时文件
Oct 28 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
利用文件属性结合Session实现在线人数统计
2006/10/09 PHP
smarty实例教程
2006/11/19 PHP
10条PHP高级技巧[修正版]
2011/08/02 PHP
关于php循环跳出的问题
2013/07/01 PHP
php数组删除元素示例
2014/03/21 PHP
简单的php+mysql聊天室实现方法(附源码)
2016/01/05 PHP
jQuery each()小议
2010/03/18 Javascript
Chrome Form多次提交表单问题的解决方法
2011/05/09 Javascript
浅析showModalDialog数据缓存问题(用禁止浏览器缓存解决)
2013/07/09 Javascript
解析javascript中鼠标滚轮事件
2015/05/26 Javascript
jQuery预加载图片常用方法
2015/06/15 Javascript
javascript实现五星评价代码(源码下载)
2015/08/11 Javascript
JavaScript实现打开链接页面的方式汇总
2016/06/02 Javascript
layui默认选中table的CheckBox复选框方法
2019/09/19 Javascript
Vue 实现分页与输入框关键字筛选功能
2020/01/02 Javascript
JavaScript中的各种宽高属性的实现
2020/05/08 Javascript
python函数参数*args**kwargs用法实例
2013/12/04 Python
从零开始学Python第八周:详解网络编程基础(socket)
2016/12/14 Python
Python闭包之返回函数的函数用法示例
2018/01/27 Python
Python3 XML 获取雅虎天气的实现方法
2018/02/01 Python
Python3.6实现连接mysql或mariadb的方法分析
2018/05/18 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
2018/12/10 Python
tensorflow 自定义损失函数示例代码
2020/02/05 Python
Python统计文本词汇出现次数的实例代码
2020/02/27 Python
浅析Python面向对象编程
2020/07/10 Python
python+excel接口自动化获取token并作为请求参数进行传参操作
2020/11/10 Python
西班牙Polo衫品牌:Polo Club
2020/08/09 全球购物
代码中finally中的代码会不会执行
2012/02/06 面试题
文秘专业个人求职信
2013/12/22 职场文书
残疾人创业典型事迹
2014/02/01 职场文书
应届生找工作求职信
2014/06/24 职场文书
公司庆典欢迎词
2015/01/26 职场文书
2016年七夕爱情寄语
2015/12/04 职场文书
《三国志》赏析
2019/08/27 职场文书
Nginx + consul + upsync 完成动态负载均衡的方法详解
2021/03/31 Servers
Matplotlib绘制条形图的方法你知道吗
2022/03/21 Python