Skip to main content

dynamically reorder functions

Project description

Concept

Functions in a module should be laid out like a newspaper

Most important (the headline) at the top, then subsequent lower layers each with finer detail as we go down.

Functions with no level of indentation should be considered top level

Functions that are not called within the module can be considered the user end points for the module because if they are not called within the module then the intention is to call them from elsewhere.

So we order top level functions by the number of times they are called from lowest to highest count.

Of those, functions that call the most number of other functions in the same module should be near the top as they are pulling together the most amount of module specific processing.

lastly if the function is called the same as another and calls the same number of other functions then we order by name alphabetically.

Sort methods

newspaper

newspaper: functions that have the least calls themselves and call the most functions, if functions equally ranked with these metrics they are sorted alphabetically.

The idea is to read functions that are both not called within the module (thus are expected to be called elsewhere) and call the most functions from within the module, giving you the "headline" of the modules functionality while having to read the least but allow you to read internal functions with more detail if necessary in order of importance for the module.

called

called: functions that are called the most are at the top moving to those that are called the least. The idea is to get an idea of the core functionality of a module before reading the code where they are used.

calls

calls: functions that call the most other functions are at the top allowing for efficient reading of the code that "do" the most first.

alphabetical

alphabetical: self-explanatory, excludes leading underscores

example: ["a", "_b", "c"]

alphabetical_include_leading_underscores

alphabetical_include_leading_underscores: self-explanatory, includes leading underscores

example: ["_b", "a", "c"]

Leading underscores

If a function is:

  • internal to another (nested) it is given a leading underscore.
  • called by another in the same module it is given a leading underscore.

Otherwise it has no leading underscore.

To use

python3 -m headline <cwd> [src_dir] [tests_dir] [sort_type] [tests_only] [rename] [suffix]

args

  • cwd: the directory to run the process on

  • src_dir: the directory with the source files, headline will match files in this directory to those with the test_ prefix in the specified tests directory

  • test_dir: the directory where the test files are for the source files

  • sort_type:

    • newspaper: sort functions based on a custom "newspaper" logic.
    • called: sort functions by the number of times they are referenced or "called" by other functions
    • calls : sort functions based on the number of references or "calls" they make to other functions
    • alphabetical: sort functions alphabetically, ignoring leading underscores
    • alphabetical_include_leading_underscores: sort functions alphabetically, including leading underscores in the sort order
  • rename: add leading underscores to functions that are used internally within the module

  • suffix: if you want to try headline on your code but not apply the changes inplace, this will create files with the suffix you specify, so my_file.py with suffix _example will have headline applied to it and save to my_file_example.py

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

headline-0.0.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

headline-0.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file headline-0.0.1.tar.gz.

File metadata

  • Download URL: headline-0.0.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for headline-0.0.1.tar.gz
Algorithm Hash digest
SHA256 470e19db50aa75fcf8d04d29c82217ed9b146b02288b6cd3fb29b1d1c1d640c3
MD5 e126459065b56448fd3140ab097dedd9
BLAKE2b-256 1db00e65308b58073dab90ab043b2cace4e596d7c6a4ef0320fab2a810bbc945

See more details on using hashes here.

Provenance

The following attestation bundles were made for headline-0.0.1.tar.gz:

Publisher: publish.yaml on second-ed/headline

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file headline-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: headline-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for headline-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 100a28b1b4ea06235152fc01bf23579adcc15a0f49fdcd607a385faf68ac0156
MD5 b4ff629023d07e79f5fec9a7ba908847
BLAKE2b-256 3d33a66b33dedd53addee36165da147d9437c7911041a02563fc4c366d0936e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for headline-0.0.1-py3-none-any.whl:

Publisher: publish.yaml on second-ed/headline

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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