Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite pad_sort_key to account for alphanumeric pads. #17

Merged
merged 3 commits into from
Mar 20, 2024

Conversation

sabogalc
Copy link
Contributor

This PR rewrites the pad_sort_key function to account for pad names such as A0 found on BGA packages. Below is a before and after with U30 on Glasgow. Previously, the pads were ordered
A1, A10, A11, A2, A3....
but now they are in the proper order.
Screenshot 2024-03-19 011926
Screenshot 2024-03-19 203507

Rewrote pad_sort_key to account for alphanumeric pads.
else:
return (1, name)
parts = re.split('([0-9]+)', pad_name)
alphanumeric_parts = [f"{int(text):09d}" if text.isdigit() else text for text in parts if text]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will just as well, without hardcoding numbers:

Suggested change
alphanumeric_parts = [f"{int(text):09d}" if text.isdigit() else text for text in parts if text]
alphanumeric_parts = [text[::-1] if text.isdigit() else text for text in parts if text]

(It reverses the digit order by using a weird bit of Python syntax. It's basically str(reversed(text)) but in one operation.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it and it still works great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize, I forgot to hit "Refresh Plugins", so even though I put your code into my plugins path, I was still running the previous code. Your solution results in this.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I may need to open a new PR but I will wait on confirmation from you to see if this can be reopened on your end.

I apologize for all of the disorder caused by my PRs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this PR cannot be reopened since it was merged.

I will encourage you to learn how to use Git.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'm very sorry for the mix-up. I'm much better at hardware than I am software. I will make a new PR for this just so that you don't have to be reliant on the .obdata one to fix this issue.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also yeah there's a logic error, I think this should actually work:

import re # at the beginning of file

def natural_sort_key(s):
    return [int(text) if text.isdigit() else text.casefold()
            for text in re.compile('([0-9]+)').split(s)]

Co-authored-by: Catherine <whitequark@whitequark.org>
@whitequark whitequark merged commit cb4b75d into whitequark:main Mar 20, 2024
@sabogalc sabogalc deleted the padSortFix branch March 20, 2024 20:36
@sabogalc sabogalc restored the padSortFix branch March 20, 2024 20:53
sabogalc added a commit to sabogalc/kicad-boardview that referenced this pull request Mar 20, 2024
Implemented natural_sort_key fix from whitequark#17 (comment)
set-soft added a commit to INTI-CMNB/KiBot that referenced this pull request Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants