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二维码生成库qrcode安装和使用示例
Dec 16 Python
python实现同时给多个变量赋值的方法
Apr 30 Python
Fiddler如何抓取手机APP数据包
Jan 22 Python
Python实现的多叉树寻找最短路径算法示例
Jul 30 Python
树莓派用python中的OpenCV输出USB摄像头画面
Jun 22 Python
python打印直角三角形与等腰三角形实例代码
Oct 20 Python
pytorch:torch.mm()和torch.matmul()的使用
Dec 27 Python
PyTorch中permute的用法详解
Dec 30 Python
解决pymysql cursor.fetchall() 获取不到数据的问题
May 15 Python
Matplotlib自定义坐标轴刻度的实现示例
Jun 18 Python
十个Python自动化常用操作,即拿即用
May 10 Python
python unittest单元测试的步骤分析
Aug 02 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/04/25 PHP
php获取mysql数据库中的所有表名的代码
2011/04/23 PHP
PHP使用curl制作简易百度搜索
2016/11/03 PHP
动态加载script文件的两种方法
2013/08/15 Javascript
原生js实现日期联动
2015/01/12 Javascript
JavaScript中return false的用法
2015/03/12 Javascript
JavaScript中的lastIndexOf()方法使用详解
2015/06/06 Javascript
分享我的jquery实现下拉菜单心的
2015/11/29 Javascript
js保留两位小数方法总结
2018/01/31 Javascript
React注册倒计时功能的实现
2018/09/06 Javascript
NProgress显示顶部进度条效果及使用详解
2019/09/21 Javascript
微信小程序个人中心的列表控件实现代码
2020/04/26 Javascript
jquery绑定事件 bind和on的用法与区别分析
2020/05/22 jQuery
Python中的jquery PyQuery库使用小结
2014/05/13 Python
Python中类的继承代码实例
2014/10/28 Python
Python requests库用法实例详解
2018/08/14 Python
Python sklearn KFold 生成交叉验证数据集的方法
2018/12/11 Python
Python OpenCV实现鼠标画框效果
2020/08/19 Python
wxPython实现带颜色的进度条
2019/11/19 Python
Python:合并两个numpy矩阵的实现
2019/12/02 Python
利用SVG和CSS3来实现一个炫酷的边框动画
2015/07/22 HTML / CSS
使用CSS3 制作一个material-design 风格登录界面实例
2016/12/12 HTML / CSS
德国百年厨具品牌WMF美国站:WMF美国
2016/09/12 全球购物
英国顶级家庭折扣店:The Works
2017/09/06 全球购物
Linux内核产生并发的原因
2012/07/13 面试题
上课说话检讨书大全
2014/01/22 职场文书
素食餐饮项目创业计划书
2014/02/02 职场文书
大队干部竞选演讲稿
2014/04/28 职场文书
市场开发计划书
2014/05/07 职场文书
三八红旗集体先进事迹材料
2014/05/22 职场文书
八项规定自查自纠报告及整改措施
2014/10/26 职场文书
全国助残日活动总结
2015/05/11 职场文书
审查起诉阶段律师意见书
2015/05/19 职场文书
春节慰问简报
2015/07/21 职场文书
简单谈谈Python面向对象的相关知识
2021/06/28 Python
SQL Server中使用表变量和临时表
2022/05/20 SQL Server