question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Finalize sorting of class bodies

See original GitHub issue

Before sorting topologically, ssort currently pre-sorts class members in the following order:

  • Docstring.
  • Special attributes (__slots__ and __doc__).
  • Regular class attributes.
  • Lifecycle d’under methods (e.g. __init__ or __del__) in fixed order.
  • Regular methods in original order.
  • All other d’under methods, in fixed order.

I think this is basically sensible but, unlike topological sorting of top-level statements, there is no strong argument for it to be this way.

I’d like to invite comments or opinions on how this order could be improved before the 1.0 release.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
drcongocommented, Feb 17, 2022

I quite like @AnonymouX47’s order, it’s not dissimilar to ours and makes sense to my brain, but yeah, it’s likely to be a very personal preference for most users.

1reaction
AnonymouX47commented, Feb 17, 2022

The fact is… no matter how much deliberation happens, the group order chosen will be opinionated in one way or the other.

So, I propose that a reasonable default order be chosen but beyond that, a user should be able to customize the grouping and group ordering as desired and even (maybe later) be able to include user-defined groups and change the group a statement belongs to.


So, ssort can define a default set of groups and a default group ordering for top-level statements and class members separately.

For the custom group ordering, there could be:

  • a CLI option or config option for a global setting
  • a particularly-styled comment at the top of a module (after imports) for module-specific setting
  • a particularly-styled comment at the top of a class (after the docstring) for class-specific setting

For the addition of custom groups and to change the group a statement belongs:

  • a particularly-styled comment on the same line as a statement (or its header, for definitions) or just immediately above the statement.

A statement can belong to only one group.


The particularly-styled comment can be of the form:

# ssort: <group1>, <group2>, <group3>

for custom group ordering

and

# ssort: <group>

for custom grouping. If <group> isn’t among the default, then it’s added to the set of groups for that module or class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

bwhmather/ssort: Tool for automatically sorting python ...
Tool for automatically sorting python statements within a module - GitHub ... ssort will sort top level statements and statements in class bodies....
Read more >
Why I have access to finalize() method in other sub class of ...
When the finalize() method is called (because a System.gc() has been requested), the method will first be looked for in the SubClass class,...
Read more >
Scripting API: SortingGroup - Unity - Manual
The SortingGroup component solves this by ensuring the entire branch of the character are sorted and rendered together.
Read more >
Learn Java Programming - The Finalize Method Tutorial
In my Garbage Collection tutorial, I discussed how Java uses the garbage collector to free up memory on the heap. Objects are stored...
Read more >
How To Sort An Array In Java - Tutorial With Examples
The sort method provided by 'java.util.Arrays' class is a very simple and faster way to sort an array. This method can sort elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found