Skip to main content

Sort methods, imports and classes in a python source file

Project description

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

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

sortpythonmethods-12.tar.gz (5.1 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for sortpythonmethods-12.tar.gz
Algorithm Hash digest
SHA256 6d6daec6f540d995dd87aca3af211af7a0be721cfbb557674ced3ef1385c144c
MD5 e7b21e2124c66ce17b5a1a0bd73f6fbd
BLAKE2b-256 7dc4f1d87c34e9a4463d301c0de14f091ede45342e058dfc5b1f9595b52aa094

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