Skip to main content

Rearrange methods alphabetically in a Python source file. - Python3

sortorder

  1. docstring

  2. future imports

  3. imports first on alphabet then on length (apo, alpha-pipe-organ)

  4. from, modules on apo, imports on alpa

  5. classes sorted on baseclassname then on classname alpha (internals of class unchanged)

  6. methods sorted on methodname alpha

pep8

Can be combined with cp-pep8 for consistent readable code

sortpythonmethods -f myfile.py -w
python2 ~/cp.py -f myfile.py

example

Source file before sort

"""
Docstring is required
"""
import sys
import http
import email
import concurrent.futures
import os

class Zorro(object):
    pass

class actie(object):
    pass

def applemethod():
    pass
def foobar():
    pass
class Alpha1(object):
    pass


def helloworld():
    pass

class ZZ(object):
    pass


class Alpha2(ZZ):
    pass

Run command

sortpythonmethods -f myfile.py

Sorted source file

#!/usr/bin/env python3
# coding=utf-8
"""
Docstring is required
"""

import email
import http
import os
import sys
import concurrent.futures

class ZZ(object):
    pass


class Alpha1(object):
    pass


class Alpha2(ZZ):
    pass


class Zorro(object):
    pass


class actie(object):
    pass


def applemethod():
    pass


def foobar():
    pass


def helloworld():
    pass

completeness

  • baseclasses

  • globals

  • globalmethod calls

  • comments

todo

  • global pycharm directives

screenshot

Sorted methods in IntelliJ

sorted_methods

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sortpythonmethods-17.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file sortpythonmethods-17.tar.gz.

File metadata

File hashes

Hashes for sortpythonmethods-17.tar.gz
Algorithm Hash digest
SHA256 3e49344b382d5fa82b4068e8005de7bb6963eecbb7309b08d9a363601987c073
MD5 65905d676fe6306e5a0519441064a2ef
BLAKE2b-256 7b968045558ad466f8e5b62499b3e06ec402dcafa5ddac11d9bc3faef89c9ce7

See more details on using hashes here.

Release history Release notifications | RSS feed

47

1 file

44

1 file

43

1 file

42

1 file

41

1 file

40

1 file

39

1 file

38

1 file

37

1 file

35

1 file

34

1 file

33

1 file

32

1 file

31

1 file

30

1 file

28

1 file

27

1 file

26

1 file

25

1 file

24

1 file

23

1 file

22

1 file

21

1 file

20

1 file

19

1 file

18

1 file

This release

17 This release

1 file

16

1 file

15

1 file

14

1 file

13

1 file

12

1 file

11

1 file

9

1 file

8

1 file

7

1 file

6

1 file

5

1 file

3

1 file

2

1 file

1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page