Package: nouvelordre
A tool forreordering import statementsinside Python code blocks.This project lives on github:github html page:http://feth.github.com/nouvelordre/github classic page:https://github.com/feth/nouvelordrerepository:$ git clone git://github.com/feth/nouvelordre.gitIt also lives on pypi:
*http://pypi.python.org/pypi/nouvelordre/Getting startedCommon usage‘reorder’is an executable script:$ nouvelordre.py --infile mymodule.py --outfile rewritten.pyis equivalent to:$ nouvelordre.py < mymodule.py > rewritten.pyYou may want to rewrite all files in a directory, say ‘/home/toto/project’:$ for file in $(find '/home/toto/project') ; do reorder -i $file -o $file ; done
$ <run your test suite!>
$ git diffof course, if you run the above, you are using version control so you can check the diff and revert modifications in case of troubleInstallationThe installation procedure is not very good for the moment but should work.From source:$ python setup.py installFrom the internet (Pypi -http://pypi.python.org):$ pip install nouvelordreAs a side note, I suggest installing in avirtualenv-this means running the following before installing as above, therefore not polluting your system:$ virtualenv nouvelordre
$ cd nouvelordre
$ source ./bin/activateOptionsoptions:--infile INFILE, -i INFILE            a Python source file, defaults to standard input
--outfile OUTFILE, -o OUTFILE         output, defaults to standard output
--dump, -d                            in case of failure, keep processing data in files
--version, -v                         prints version and exitsFeaturesBlocks separated by anything (white line for instance) are considered separately.Statements inside a block are reorderedImports in one statement are reordered.
Example:import sys, osbecomes:import os
import sysor:from module import b, a as f, cbecomes:from module import a as f, b, cTries and avoid damaging your precious Python work (see below, Disaster avoidance)LimitationsThis script will only handle first level statements (ie. not indented statements).This script will not handle several “import module” for the same module in the same block.Some files are not compilables by ast.parse() (help appreciated).DependancesCompulsoryPython 2.6 at least (for ast)Python 2.7 or python-argparseRecommendedenthought’s python-traits - provides type verification and may catch some bugs (absolutely compulsory if you intend to patch this software).Exit codes0 if all is well.129 if NotImplementedError (an import on the same line as another instruction, separated by ‘;’).130 if ast.parse was not able to compile the file.Disaster avoidanceThis software is not perfect and might kill your golden retriever or ruin your diploma but I have tried hard for this never to happen.‘reorder’performs 2 passes. Pass 1 is performed on the original input; pass 2 is performed on the result of pass 1.
If any error occurred during either pass, the program exits and leaves your files untouched.
If pass 1 and 2 would give a different result, the program exits and leaves your files untouched. This is not a 100% guarantee, but it ensures the file stays compilable and all optimizations were performed.Just because I prefer my program to admit that it failed than to shred your beautiful Python source code.