python -m pythontidy2 [-t expandtabsize] script.py
pip install pythontidy2
Life is short, format the code automatically
(人生苦短,自动排版)
Tidy python scripts
Convert Tabs to Spaces(Tabs sized can be decide in argument -t, default is 4)
Delete white spaces at end of line
###Own thoughts about code format, some part are different from PEP8
PEP8: Whitespace in Expressions and Statements Not compliant
Always add 1 white space between operators
Align dict and assignment statments like following
list = [1, 2, {
'Alicedefg': '2341',
'Beth' : "c",
'Cecil' : '3258',
}, 4]
list = [1, 2, {
'Alicedefg' : '2341',
'Beth' : "c",
'Cecil' : '3258',
}, 4]
doc = ""
long_variable = ((doc+' ') if doc else '')
x = ""
doc = ""
long_variable = ((doc + ' ') if doc else '')
x = ""
Only Windows(CRLF) Linux/Modern OSX(LF) end of line mark supported
Do not support classic Mac end of line mark(CR)
Widechar(Asian) in sorce code (as variable or dict Key) not tested
Below example binds the tidy fucntion to F7
command -range=% -nargs=* TidyPython <line1>,<line2>!python -m pythontidy2
fun DoTidyPython()
let Pos = line2byte( line( "." ) )
:TidyPython
exe "goto " . Pos
endfun
au Filetype python nmap <buffer> <F7> :call DoTidyPython()<CR>
au Filetype python imap <buffer> <F7> <ESC>:call DoTidyPython()<CR>
au Filetype python vmap <buffer> <F7> :TidyPython<CR>
BSD
Copyright (c) 2016, Boying Xu All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.