how to format python code

参考这个帖子:

how-do-i-autoformat-some-python-code-to-be-correctly-formatted

主要有下面三个方法:

  • VIM plugin
    In vim, plugin python-mode offers the command :PyLintAuto using autopep8.

    pip install autopep8
    autopep8 your_script.py    # dry-run, only print
    autopep8 -i your_script.py # replace content
    
  • yapf
    yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.

    pip install yapf
    yapf your_script.py    # dry-run, only print
    yapf -i your_script.py # replace content
    
  • Use reindent.py
    It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package.

    # find . -type f -name "*.py" | xargs python reindent.py --nobackup
    

试用了autopep8 和 yapf,效果都不错,但二者结果也有些差别,