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

Argcomplete

This project is equiped with argcomplete which you can enable via running this:

eval "$(register-python-argcomplete alphasort)"

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.21.tar.gz (5.0 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.21-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alphasort-0.1.21.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Linux/6.5.0-1025-azure

File hashes

Hashes for alphasort-0.1.21.tar.gz
Algorithm Hash digest
SHA256 78258b7d01efb82a7268ea3b7fd56b63fd35c9e9af04b901b23971c2c6507fe3
MD5 b462e9dcb0691e8b35cbec563949f9f0
BLAKE2b-256 209066a7592c68c4e220878c24c8bf108a2e2d24cfa8b9c5845724fa03c86997

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alphasort-0.1.21-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Linux/6.5.0-1025-azure

File hashes

Hashes for alphasort-0.1.21-py3-none-any.whl
Algorithm Hash digest
SHA256 8485e88960e412be066acb58513b75ea93e0d1ad38f78d159e3cf30e4c9672f8
MD5 ef9ea0986b7febc984165d49fbb78607
BLAKE2b-256 48bf8b2aca767ad68dedb8725003555117fcbb54f862c6b4fa13e97863b97395

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