Cleans up, regularizes, and reformats the text of Python scripts.
This script reads Python code from standard input and writes a revised version to standard output.
Alternatively, it may be invoked with file names as arguments:
python PythonTidy.py input output
Suffice it to say that input defaults to ‘-‘, the standard input, and output defaults to ‘-‘, the standard output.
It means to encapsulate the wisdom revealed in:
o Rossum, Guido van, and Barry Warsaw. “PEP 8: Style Guide for Python Code.” 23 Mar. 2006. Python.org. 28 Nov. 2006 <http://www.python.org/dev/peps/pep-0008/>.
Python scripts are usually so good looking that no beautification is required. However, from time to time, it may be necessary to alter the style to conform to changing standards. This script converts programs in a consistent way. It abstracts the pretty presentation of the symbolic code from the humdrum process of writing it and getting it to work.
This script assumes that the input Python code is well-formed and works to begin with. It doesn’t check. If all goes well, the output Python code will work, too. Of course, you are advised to test it fully to be sure.
If it means anything to you, know then that this script mangles literals, converting them to a canonical form, e.g., hex integers are converted to decimal and raw strings are converted to escaped strings. This negatively impacts code readability, but the price paid for piggybacking on the standard parser must be borne.
This script should be run only by python.2.5 (and perhaps higher) on scripts written for that version (and perhaps lower) because of its limited knowledge of and expectations for the abstract syntax tree node classes returned by the compiler module. It wouldn’t hurt much to try it from (and on) other versions, though, and it might actually work.
Search this script for “Python Version Dependency.”
Most of the Python 2.5 test suite passes through PythonTidy.py unimpaired. Here are some tests that dont:
test_cProfile.py test_dis.py test_doctest.py test_grammar.py test_inspect.py test_pep263.py test_profile.py test_sys.py test_trace.py
The more esoteric capabilities of PythonTidy.py had to be turned off to avoid corrupting the test-suite code. In practice, you’ll want to run with PERSONAL = True to use all the functionality, and of course you’ll have the good taste to find and patch all the glitches it introduces.