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

For logging, do not use f-strings. Use % for performance reasons etc. #1205

Closed
ReimarBauer opened this issue Sep 13, 2021 · 11 comments · Fixed by #1683
Closed

For logging, do not use f-strings. Use % for performance reasons etc. #1205

ReimarBauer opened this issue Sep 13, 2021 · 11 comments · Fixed by #1683
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@ReimarBauer
Copy link
Member

refactor all logging strings

@ReimarBauer ReimarBauer added enhancement New feature or request good first issue Good for newcomers labels Sep 13, 2021
@ReimarBauer ReimarBauer added this to the 6.0.0 milestone Sep 13, 2021
@engineerrbae
Copy link

engineerrbae commented Sep 13, 2021

Hey Reimar, to show you a sample I have added % to the below code. Is this how you wish to have changes in all the .py files

https://github.com/engineerrbae/MSS/blob/develop/conftest.py#L189

summary = "\n".join(["PyQt5.QtWidgets.QMessageBox.%s: %s" %(box()._extract_mock_name(),box.mock_calls[:-1])
for box in [q, i, c, w] if box.call_count > 0])
warnings.warn("An unhandled message box popped up during your test!\n%s" %(summary))

OR

summary = "\n".join(["PyQt5.QtWidgets.QMessageBox.{ }: { }".format(box()._extract_mock_name(),box.mock_calls[:-1])
for box in [q, i, c, w] if box.call_count > 0])
warnings.warn("An unhandled message box popped up during your test!\n{ }".format(summary))

Once you share I will move ahead with refactoring.

Thanks.

@ReimarBauer
Copy link
Member Author

We do want to change only the use of f-string in logging....() to % syntax. On all other places f-strings are better.

Joern wrote an explanaition copied from #1202 (comment)

f-formatting is almost always faster than c-style formatting due to some particularly interesting implementation shenanigans. That is not the point here, however.

The idea here is that the formatting will not take place at all in case the logging level is above debug. Using an f-string always does the formatting even if nothing will be output. That can have an detrimental effect on performance with logging disabled.
Thus it is good style to always employ conditional formatting, which by necessity of backwards-compatability, is c-style formatting. For info/warn/error, which are basically always dumped or where the formatted string is needed anyway (e.g. for a popup message), one may deviate at discretion.

There is a "style" parameter, which can be triggered on a by call basis to use other formatters (e.g. the {} one). See
https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
This seems very cumbersome for little gain.
f-strings cannot be used as the decision whether to format cannot be controlled by the logging class.

@engineerrbae
Copy link

Got it. f-string is an expression that is evaluated at runtime and it lacks logging optimizations. Also, logging by default follows % standards & adoption of its default standards going to make the logging fast & accurate & better statistically.

Can you share with me the directories wherein logging is part of the files? I will make the changes, please assign me this issue. I will do it.

@ReimarBauer
Copy link
Member Author

Hi
I show how you can do this in pycharm-community after you opened the MSS dir. vscode also has this capabilties.
You can use there a text search or also one based on regular expressions,
f-string_logging

The regex I used:
\blogging\.\w.*\(f

After doing these changes you can verify the PEP8 style used in your local clone of the forked repo,see
https://mss.readthedocs.io/en/stable/development.html#forking-the-repo

github will do too ;)

@engineerrbae
Copy link

Noted. Thanks a lot.

I have started making changes at my end. And, what's the deadline to submit?

@ReimarBauer
Copy link
Member Author

there is no deadline, it may be added to 6.0 or 7.0

@kawalpreettkaur
Copy link

@ReimarBauer Added % syntax to the logging files.

@DipanshuShukla
Copy link

i will be taking up this issue

@Ritwikrajsingh
Copy link

I want to work on this issue

DipanshuShukla added a commit to DipanshuShukla/MSS that referenced this issue Apr 17, 2022
…SS#1205

converted all f-strings to % for logging an mentioned in issue Open-MSS#1205
@ReimarBauer
Copy link
Member Author

@Ritwikrajsingh have a look on the PR list @DipanshuShukla is already working on this one

ReimarBauer added a commit to Ritwikrajsingh/MSS that referenced this issue Apr 28, 2022
@ReimarBauer ReimarBauer modified the milestones: 7.0.0, 8.0.0 Jun 3, 2022
@nilupulmanodya
Copy link
Collaborator

I want to work on this.. please assign me @ReimarBauer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants