python Autopep8实现按PEP8风格自动排版Python代码

 更新时间:2021年03月02日 11:34:27   作者:Data_IT_Farmer  
这篇文章主要介绍了python Autopep8实现按PEP8风格自动排版Python代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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 eval()和exec()函数使用详解

    Python eval()和exec()函数使用详解

    exec函数执行的是python语句,没有返回值,eval函数执行的是python表达式,有返回值,exec函数和eval函数都可以传入命名空间作为参数,本文给大家介绍下Python eval()和exec()函数,感兴趣的朋友跟随小编一起看看吧
    2022-11-11
  • Python对字符串实现去重操作的方法示例

    Python对字符串实现去重操作的方法示例

    字符串去重是python中字符串操作常见的一个需求,最近在工作中就又遇到了,所以下面这篇文章主要给大家介绍了关于Python对字符串实现去重操作的相关资料,文中给出了详细的介绍,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • Python使用三种方法实现PCA算法

    Python使用三种方法实现PCA算法

    这篇文章主要介绍了Python使用三种方法实现PCA算法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • python字符串过滤性能比较5种方法

    python字符串过滤性能比较5种方法

    这篇文章主要介绍了python字符串过滤性能比较5种方法的相关资料,需要的朋友可以参考下
    2017-06-06
  • python PyQt5 爬虫实现代码

    python PyQt5 爬虫实现代码

    这篇文章主要介绍了python PyQt5 爬虫实现代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Django中使用 Closure Table 储存无限分级数据

    Django中使用 Closure Table 储存无限分级数据

    对于数据量大的情况(比如用户之间有邀请链,有点三级分销的意思),就要用到 closure table 的结构来进行存储。这篇文章主要介绍了Django中使用 Closure Table 储存无限分级数据,需要的朋友可以参考下
    2019-06-06
  • 利用Python统计Jira数据并可视化

    利用Python统计Jira数据并可视化

    目前公司使用 Jira 作为项目管理工具,在每一次迭代完成后的复盘会上,我们都需要针对本次迭代的 Bug 进行数据统计,以帮助管理层能更直观的了解研发的代码质量。本篇文章将介绍如何利用统计 Jira 数据,并进行可视化,需要的可以参考一下
    2022-07-07
  • python实现定时播放mp3

    python实现定时播放mp3

    这篇文章主要介绍了python实现定时播放mp3,程序非常简单,功能很实用,主要是使用python实现了一首mp3歌每半小时播放一次,有需要的小伙伴可以参考下。
    2015-03-03
  • python Tornado框架的使用示例

    python Tornado框架的使用示例

    这篇文章主要介绍了python Tornado框架的使用示例,帮助大家更好的利用python进行web开发,感兴趣的朋友可以了解下
    2020-10-10
  • 通过代码实例了解Python sys模块

    通过代码实例了解Python sys模块

    这篇文章主要介绍了通过代码实例了解Python sys模块,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09

最新评论