Skip to main content

Sort lines within regions of a file.

Project description

Alphasort

Problem Statement

Keeping lines (alphabetically) sorted is always a pain. Say we have a long list that we want to maintain alphabetical sorting on (for the sake of readability and maintenance).

animals = [
    "alligator",
    "monkey",
    "zebra",
]

Now We want to add “cat” to this list...

Either:

  1. dev remembers to insert in correct location
  2. dev forgets and PR reviewers have to leave comment about it (just another thing prolonging PR time)
  3. the list falls out of sorting for a while then gets included as a refactor in a PR, adding many lines changed which inflates PR size and leads to unnecessary merge conflicts

Introducing Alphasort

We add macros (directives) and a tool that can parse the files for this and to sort

animals = [
    # alphasort: on
    "apple",
    "monkey",
    "zebra",
    # alphasort: off
]

And yes, it works on most other files too, including comment deliminators like // and more. It even works with json if you do "_comment": "alphasort: on"

Setup

pip install alphasort

Usage

alphasort "./path/**/*.py"

Also recommend adding it

  • to your IDE on-save commands
  • to your pre-commit
  • to your CI checks

P.S.

This is inspired by other such macros like:

# fmt: off
mylist = [3, 2, 1]  # Black will not touch this line.
# fmt: on

I’m honestly surprised something like this doesn’t exist. But here it is now. So, you're welcome 😁

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alphasort-0.1.20.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alphasort-0.1.20-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file alphasort-0.1.20.tar.gz.

File metadata

  • Download URL: alphasort-0.1.20.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Darwin/23.5.0

File hashes

Hashes for alphasort-0.1.20.tar.gz
Algorithm Hash digest
SHA256 f3e08ed5c55a7e83ff388459c9a503f69f15bf94c142edee486dd53212080adc
MD5 f994cfff36e58a42b399314070378cbc
BLAKE2b-256 365c4b7a6eb04103a29648886f4adb3d8ce4bcc16df0f879938e425ebe6aaa63

See more details on using hashes here.

File details

Details for the file alphasort-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: alphasort-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Darwin/23.5.0

File hashes

Hashes for alphasort-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 6345224c197e42b5eb5d0555945c75b8e7cb6c910e56e0bc22ebd6e79edef71b
MD5 0c7025b8f3fda59a872acb6b8ef0846d
BLAKE2b-256 a4ecd4f9a49206c547c30c41831d4547fc2f7f3c24ff19fd9f3f2f4402605e20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page