New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

alphasort

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alphasort

Sort lines within regions of a file.

  • 0.1.21
  • PyPI
  • Socket score

Maintainers
1

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 😁

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc