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爬虫之爬虫的定义及URL构成
Nov 04 Python
使用python批量化音乐文件格式转换的实例
Jan 09 Python
Python实现 PS 图像调整中的亮度调整
Jun 28 Python
使用Python实现画一个中国地图
Nov 23 Python
Python Numpy数组扩展repeat和tile使用实例解析
Dec 09 Python
python飞机大战pygame碰撞检测实现方法分析
Dec 17 Python
如何基于python3和Vue实现AES数据加密
Mar 27 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
如何用python实现一个HTTP连接池
Jan 14 Python
python 实现Requests发送带cookies的请求
Feb 08 Python
解决pycharm下载库时出现Failed to install package的问题
Sep 04 Python
numpy array找出符合条件的数并赋值的示例代码
Jun 01 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
如何设置mysql允许外网访问
2013/06/04 PHP
PHP has encountered a Stack overflow问题解决方法
2014/11/03 PHP
laravel 5.4 + vue + vux + element的环境搭配过程介绍
2018/04/26 PHP
jquery获得option的值和对option进行操作
2013/12/13 Javascript
JS显示表格内指定行html代码的方法
2015/03/31 Javascript
jQuery简单获取键盘事件的方法
2016/01/22 Javascript
关于Javascript中defer和async的区别总结
2016/09/20 Javascript
js实现拖拽功能
2017/03/01 Javascript
js实现添加删除表格(两种方法)
2017/04/27 Javascript
微信小程序 转发功能的实现
2017/08/04 Javascript
利用JavaScript的%做隔行换色的实例
2017/11/25 Javascript
Vue项目部署的实现(阿里云+Nginx代理+PM2)
2019/03/26 Javascript
《javascript设计模式》学习笔记五:Javascript面向对象程序设计工厂模式实例分析
2020/04/08 Javascript
vant 解决tab切换插件标题样式自定义的问题
2020/11/13 Javascript
[06:43]2018DOTA2国际邀请赛寻真——VGJ.Thunder
2018/08/11 DOTA
python pdb调试方法分享
2014/01/21 Python
在Django框架中运行Python应用全攻略
2015/07/17 Python
Python学习教程之常用的内置函数大全
2017/07/14 Python
python二维列表一维列表的互相转换实例
2018/07/02 Python
在python Numpy中求向量和矩阵的范数实例
2019/08/26 Python
python下载卫星云图合成gif的方法示例
2020/02/18 Python
Python类的继承super相关原理解析
2020/10/22 Python
cookies应对python反爬虫知识点详解
2020/11/25 Python
python 读取yaml文件的两种方法(在unittest中使用)
2020/12/01 Python
CSS3 calc()会计算属性详解
2018/02/27 HTML / CSS
ivx平台开发之不用代码实现一个九宫格抽奖功能
2021/01/27 HTML / CSS
纽约现代艺术博物馆商店:MoMA STORE(室内家具和杂货商品)
2016/08/02 全球购物
英国领先的野生鸟类食品供应商:GardenBird
2018/08/09 全球购物
花卉与景观设计系大学生求职信
2013/10/01 职场文书
体育教育专业自荐信范文
2013/12/20 职场文书
高一化学教学反思
2014/02/05 职场文书
党校培训自我鉴定范文
2014/03/20 职场文书
工伤事故处理协议书怎么写
2014/10/15 职场文书
法定代表人免职证明
2015/06/24 职场文书
聊聊pytorch测试的时候为何要加上model.eval()
2021/05/23 Python
如何理解及使用Python闭包
2021/06/01 Python