SSort – automatically organise Python statements

SSort is a command-line utility that automatically reorganises statements in Python source files according to their dependencies. It aims to make Python modules easier to navigate by placing definitions before the code that depends on them.

In larger Python projects, functions, classes and other statements may appear in an inconsistent order. Understanding where a particular function is defined can require considerable scrolling or repeated use of an editor’s navigation facilities.

SSort applies a bottom-up approach to source code organisation. Lower-level definitions appear earlier in a module, while statements depending on those definitions appear further down. This follows Python’s execution model, where statements are evaluated sequentially when modules are loaded.

The utility also organises statements inside classes, applying conventions for attributes, constructors, public methods and other class members. It deliberately avoids imposing unnecessary grouping decisions where no dependencies exist.

SSort complements tools such as Black and isort. Rather than replacing their formatting capabilities, it concentrates on the structural ordering of Python statements.

This is free and open source software.

Key Features

  • Automatic reordering of Python module statements according to their dependencies.
  • Topological sorting places dependent statements after the definitions they require.
  • Preserves the original relative ordering of statements where no dependencies exist.
  • Maintains statement ordering within circular dependency groups.
  • Reorganises class bodies using predefined conventions for different member types.
  • Groups class docstrings, special attributes, inner classes and regular attributes.
  • Positions lifecycle methods such as __init__ and __new__ before ordinary public methods.
  • Applies dependency-based ordering to private methods within classes.
  • Preserves necessary class-definition-time dependencies when reorganising members.
  • Check mode for identifying incorrectly ordered source files without making changes.
  • Diff mode displaying proposed modifications before files are rewritten.
  • Integration with pre-commit for automatic source organisation checks.
  • Works alongside Black and isort to maintain both structural and stylistic consistency.
  • Designed to minimise unnecessary changes to source files when reorganising statements.
  • Cross-platform support – runs on Linux, macOS, and Windows.

Website: github.com/bwhmather/ssort
Support:
Developer: Ben Mather
License: MIT License

SSort is written in Python. Learn Python with our recommended free books and free tutorials.


Related Software

Python Code Formatters
RuffExtremely fast Python linter and code formatter
YAPFPython formatter based on clang-format
BlackUncompromising Python code formatter
isortPython utility and library to sort imports
autopep8Formats Python code to conform to the PEP 8 style guide
DarkerApply black reformatting to Python files
PyinkPython code formatter
blueSomewhat less uncompromising code formatter than black
μsortSorts import statements
eradicateRemove commented-out code from Python files

Read our verdict in the software roundup.


Best Free and Open Source Software LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.

We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more.

Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion.

Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README.
Subscribe

Please read our Comment Policy before commenting.

Notify of
guest
0 Comments
Oldest
Newest Most Voted