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

Cleanup f-strings, logging and formats #443

Merged

Conversation

sveinse
Copy link
Collaborator

@sveinse sveinse commented May 20, 2024

This PR changes all occurrences of % formatting and str.format() to f-strings.

Please review, as it is easy to sneak in a typo in these changes.

Copy link
Owner

@christiansandberg christiansandberg left a comment

Choose a reason for hiding this comment

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

Looked through most of them. 👍

Copy link
Collaborator

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

Only checked on the surface, could use some more time when back at a PC keyboard.

There's some semantic differences you should make sure are intended. And some opportunities to fix inconsistent formats while already touching the lines.

canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/sdo/client.py Outdated Show resolved Hide resolved
canopen/sdo/client.py Show resolved Hide resolved
@sveinse
Copy link
Collaborator Author

sveinse commented May 20, 2024

And some opportunities to fix inconsistent formats while already touching the lines.

Initially I had planned to make this a like-for-like change to ensure no changed behavior. However, if it's wanted (it seems it is) we can fix inconsistencies.

Copy link
Collaborator Author

@sveinse sveinse left a comment

Choose a reason for hiding this comment

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

@acolomb thank you for very valuable feedback 👌 . I will review and post an update.

canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
canopen/sdo/client.py Outdated Show resolved Hide resolved
sveinse and others added 3 commits May 21, 2024 01:07
* Apply suggestions from code review
* Fixup minor syntax fixes

Co-authored-by: Erlend E. Aasland <[email protected]>
@sveinse
Copy link
Collaborator Author

sveinse commented May 20, 2024

I made the liberty to merge PR #442 into this PR and close 442. The PRs did get into each other's feet.

@sveinse sveinse changed the title Cleanup: use f-strings instead of % and str.format() Cleanup f-strings, logging and formats May 20, 2024
canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/objectdictionary/__init__.py Show resolved Hide resolved
canopen/objectdictionary/eds.py Show resolved Hide resolved
Copy link
Collaborator

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

It's a lot to review, but I think I've seen all and marked additional fixes spotted.

canopen/lss.py Outdated Show resolved Hide resolved
canopen/node/remote.py Outdated Show resolved Hide resolved
canopen/node/remote.py Outdated Show resolved Hide resolved
canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/objectdictionary/__init__.py Outdated Show resolved Hide resolved
canopen/pdo/base.py Outdated Show resolved Hide resolved
canopen/pdo/base.py Outdated Show resolved Hide resolved
canopen/profiles/p402.py Show resolved Hide resolved
@sveinse
Copy link
Collaborator Author

sveinse commented May 26, 2024

It's a lot to review, but I think I've seen all and marked additional fixes spotted.

Thank you for the review. Before diving into the comments: I see that we tend to think "while at this, why can't just fix...". I'm worried that by fixing this, we mix the PR that makes no change to behavior with improvements that does. Do we care about that here? Other projects I've been involved with wants to keep them separate.

@acolomb
Copy link
Collaborator

acolomb commented May 26, 2024

In general, yes. No-op formatting or internal changes should be separated from feature work. But in this case, it'll get ridiculous to touch the same line in two PRs when it's only cleanup. Especially when there would be an intermediate step (e.g. changing to f-string first, then adjusting for hex output) which no-one cares about.

sveinse and others added 2 commits May 27, 2024 19:03
Fixes part 1

Co-authored-by: André Colomb <[email protected]>
* Change print statements to use pretty_index()
* Updates from code review
@sveinse sveinse mentioned this pull request May 27, 2024
Copy link
Collaborator

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

I think this is good to go. Maybe one last look when I'm less tired. There's two unresolved conversations left, but only with partly unrelated questions, so they shouldn't prevent this from going over the finish line.

Copy link
Collaborator

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

Two more small issues that I'd like your opinion on. @sveinse

canopen/profiles/p402.py Outdated Show resolved Hide resolved
canopen/profiles/p402.py Outdated Show resolved Hide resolved
@sveinse
Copy link
Collaborator Author

sveinse commented Jun 4, 2024

@acolomb I didn't see your latest replies about pretty_index, but as you can see, my latest commit contains a wholesale change to pretty_index for consistency. I like that we control how index:subindex is printed in one spot. I don't think the overhead is that big (nothing we do here in canopen really is).

But reading the comments, do we want to revert this one?

@acolomb
Copy link
Collaborator

acolomb commented Jun 4, 2024

But reading the comments, do we want to revert this one?

I'd vote for reverting, yes. Sorry for the confusion. I just misread your "outweighs the downsides" comment.

Note that conditional evaluation of the formatting is not really the only benefit from using the %-formatting for logging. The biggest advantage I see is that logging.Handler instances get these values passed in as distinct objects, thus can do with them whatever is necessary. Such as translating the message separately or dumping them in some other machine-readable format like JSON, ...

@sveinse sveinse force-pushed the feature-change-print-formats branch from 883af8a to 7129fed Compare June 4, 2024 18:56
@sveinse
Copy link
Collaborator Author

sveinse commented Jun 4, 2024

@acolomb I have reverted the last commit and resolved the comments regarding formatting. As far as I can tell, I think I have completed all follow-up tasks, wouldn't you agree?

Copy link
Collaborator

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

Sorry for the back and forth on the last two changes. I simply saw the index (int) being formatted as string and wanted consistency with many other places where we use 0x%04X formatting in logging calls. Calling for pretty_index() usage here feels overkill as discussed, and as you noticed yourself, there are many other places where that could be applied as well then.

Let's just harmonize on printing in hex in these two cases (like elsewhere) and call it good.

canopen/profiles/p402.py Outdated Show resolved Hide resolved
canopen/profiles/p402.py Outdated Show resolved Hide resolved
@acolomb acolomb merged commit 98283a2 into christiansandberg:master Jun 4, 2024
1 check passed
@acolomb
Copy link
Collaborator

acolomb commented Jun 4, 2024

Thank you very much for your patience and diligence in this cleanup effort! Nice to see people attending to detail and working to reduce our technical debt. 🥇

@acolomb acolomb added this to the v2.3.0 milestone Jun 11, 2024
@sveinse sveinse deleted the feature-change-print-formats branch June 12, 2024 08:13
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.

3 participants