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.

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.19.tar.gz (3.5 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.19-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alphasort-0.1.19.tar.gz
  • Upload date:
  • Size: 3.5 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.19.tar.gz
Algorithm Hash digest
SHA256 390c7a59af1b689909da09aef8fbe86382d7378fc48854ce02c122f231b9d9fd
MD5 17f3c5956a93560047e00f856aa3b781
BLAKE2b-256 652f593c23a9dcfb0b5b5380a3412182729d0e4ea4d831e6a9e6c275c30f4ed0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alphasort-0.1.19-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.19-py3-none-any.whl
Algorithm Hash digest
SHA256 87640b26500e189dea412397bace864e518a83136ae1025428960278f90ae485
MD5 5456437f2d737c28b04181d02a7864ba
BLAKE2b-256 f4f99e40cbe58c023d4e96e3afa3b1d156aab1a454353092e468cfbed34311ee

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