Skip to main content

Alphabetize variables within your files

Project description

Alphabetize

Alphabetize finds grouped lines of variables within your files and orders them alphabetically. This is useful for cleaning up codebases.

The organisation priority is:

  1. independent variables
  2. dependent variables

The variable 'a' is ordered last as it depends on the other variables.

b = 10
c = 20
d = 40
a = b + c + d

UPPERCASE and lowercase variables are separated when ordered.

A = 10
B = 20
C = 30
a = 10
b = 20
c = 30

Installation

pip install alphabetize

Usage

alphabetize myfile.py
alphabetize path/to/myfile.py

The provided argument can either be the relative path or absolute path to a Python file.

Examples

Example 1 - single use

Consider the following Python script (unordered_code.py)

import datetime
from time import time

# First Variable Block
c_variable = 60
A_variable = 10
a_variable = 40
B_variable = 20
b_variable = 50
C_variable = 30


class TestClass:
    def __init__(self):
        self.c_variable = 30
        self.a_variable = 10
        self.b_variable = 20


def test_function():
    c_variable = time()
    a_variable = 10
    b_variable = datetime
    a_list = [a_variable, b_variable, c_variable]
    bb_variable = 20
    aa_variable = 10
    cc_variable = aa_variable + bb_variable

    return a_list, cc_variable
    

Calling:

alphabetize unordered_code.py

Results in the following output:

import datetime
from time import time

# First Variable Block
A_variable = 10
B_variable = 20
C_variable = 30
a_variable = 40
b_variable = 50
c_variable = 60


class TestClass:
    def __init__(self):
        self.a_variable = 10
        self.b_variable = 20
        self.c_variable = 30


def test_function():
    a_variable = 10
    b_variable = datetime
    c_variable = time()
    a_list = [a_variable, b_variable, c_variable]
    aa_variable = 10
    bb_variable = 20
    cc_variable = aa_variable + bb_variable

    return a_list, cc_variable
    

Example 2 - multiple uses

Depending on the variable names found within grouped lines, alphabetize can be called multiple times to further reorder the grouped lines of variables.

This particularly comes into play when independent and dependent variables are mixed within the same grouped lines block.

Consider the following Python script (unordered_code_multi.py)

def test_function():
    c_variable = 30
    a_variable = 10
    b_variable = 20
    list = [a_variable, b_variable, c_variable]
    bb_variable = 20
    aa_variable = 10
    cc_variable = aa_variable + bb_variable

    return list, cc_variable
    

Calling alphabetize unordered_code_multi.py for the first time produces:

def test_function():
    a_variable = 10
    b_variable = 20
    c_variable = 30
    aa_variable = 10
    bb_variable = 20
    list = [a_variable, b_variable, c_variable]
    cc_variable = aa_variable + bb_variable

    return list, cc_variable

Then calling alphabetize unordered_code_multi.py a second time produces a further ordered file:

def test_function():
    a_variable = 10
    aa_variable = 10
    b_variable = 20
    bb_variable = 20
    c_variable = 30
    cc_variable = aa_variable + bb_variable
    list = [a_variable, b_variable, c_variable]

    return list, cc_variable

Recommended Running

When using alphabetize it is recommended that you lint and format your files in the following order:

  1. flake8 is a wrapper around the tools:
    1. Pyflakes
    2. pycodestyle
    3. Ned Batchelder's McCabe script
  2. vulture finds unused code in Python programs.
  3. alphabetize finds and orders variables within files

It is recommended to run alphabetize a second time to catch any caught dependent variables. Then a further flake8 to ensure your file is formatted and adheres to PEP8 correctly.

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

alphabetize-0.0.15.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

alphabetize-0.0.15-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file alphabetize-0.0.15.tar.gz.

File metadata

  • Download URL: alphabetize-0.0.15.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for alphabetize-0.0.15.tar.gz
Algorithm Hash digest
SHA256 60ec85f87c4ff606bf60e23b621b61b6bd8b86ec082dbb54a4b324346be209dd
MD5 292029043de13401d8990afd45352ce0
BLAKE2b-256 ff8133c06ed2f5028845e9faa393b933b1b11fc06dd9f955d0c7bcf975a098ee

See more details on using hashes here.

File details

Details for the file alphabetize-0.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for alphabetize-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 6d12d20fb74fa4429e759477a6290c843b6d0a9d08a2aba276fb5fec3f12c20d
MD5 2ace4512ede0afd797b4d42e0ea5b000
BLAKE2b-256 3917339c4c468d6f685ead8469a7ab07f68461c2d5151162de9a1f5597aebb0c

See more details on using hashes here.

Supported by

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