Skip to main content

A tool for reordering import statements inside Python code blocks.

This project lives on github:

It also lives on pypi: * http://pypi.python.org/pypi/nouvelordre/

Getting started

Common usage

‘reorder’ is an executable script:

$ nouvelordre.py --infile mymodule.py --outfile rewritten.py

is equivalent to:

$ nouvelordre.py < mymodule.py > rewritten.py

You 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 diff

of course, if you run the above, you are using version control so you can check the diff and revert modifications in case of trouble

Installation

The installation procedure is not very good for the moment but should work.

From source:

$ python setup.py install

From the internet (Pypi - http://pypi.python.org ):

$ pip install nouvelordre

As a side note, I suggest installing in a virtualenv -this means running the following before installing as above, therefore not polluting your system:

$ virtualenv nouvelordre
$ cd nouvelordre
$ source ./bin/activate

Options

options:

--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 exits

Features

  • Blocks separated by anything (white line for instance) are considered separately.

  • Statements inside a block are reordered

  • Imports in one statement are reordered. Example:

    import sys, os

    becomes:

    import os
    import sys

    or:

    from module import b, a as f, c

    becomes:

    from module import a as f, b, c
  • Tries and avoid damaging your precious Python work (see below, Disaster avoidance)

Limitations

  • This 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).

Dependances

Compulsory

  • Python 2.6 at least (for ast)

  • Python 2.7 or python-argparse

Exit codes

  • 0 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 avoidance

This 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.

Metadata

Release files for nouvelordre 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nouvelordre 0.1
File Size Uploaded
nouvelordre-0.1.tar.gz 18.2 kB Details

Release files / nouvelordre-0.1.tar.gz

Download URL nouvelordre-0.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a00476554f6b1d2e118d2397727a1363103b97f6cd05813f9544c8747a1953f
BLAKE2b-256 checksum
How to use checksums
66663cdd2524b05b213ef61c5d9f42575dbaef55e2cf2b2e30fb2c295cdc8bf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page