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 (or insert anywhere and then sort)
  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 and inflating PR size

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.

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.18.tar.gz (3.4 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.18-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for alphasort-0.1.18.tar.gz
Algorithm Hash digest
SHA256 23f7b38ee1344c0b20d54ab7511fb657a0d1031a48b722d0e4428a6b30069610
MD5 1979b946a80379a4669fffab60c4210c
BLAKE2b-256 6b3e45d30f22016da92174d1e7c248ee5a831ac9c7f817f8746f6798e9753756

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for alphasort-0.1.18-py3-none-any.whl
Algorithm Hash digest
SHA256 5e68f65719d2ca7e48e18befe24742e5182cf8f3f2317578c5be219fcb672aea
MD5 0b50fa9fa57c2557d1ac98c8aaf238b9
BLAKE2b-256 c9fdb4f1109a276d01ad38ea782cb77418b6aa9b51fef65712c3261211d5e8ba

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