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

Replace format() function with f-strings on asv/console.py #1127

Merged
merged 19 commits into from
Mar 12, 2022

Conversation

LucyJimenez
Copy link
Contributor

xref #1077
Change f-strings instead .format() on asv/console.py.

@LucyJimenez LucyJimenez changed the title Replace format() function with f-strings Replace format() function with f-strings on asv/console.py Mar 10, 2022
@LucyJimenez
Copy link
Contributor Author

@datapythonista, this PR is ready for your review. Thanks a lot!

asv/console.py Outdated
@@ -218,8 +218,7 @@ def _stream_formatter(self, record):
continued = getattr(record, 'continued', False)

if self._total:
progress_msg = '[{0:6.02f}%] '.format(
(float(self._count) / self._total) * 100.0)
progress_msg = f'[{(float(self._count) / self._total) * 100.0:6.02f}%] '
Copy link
Member

Choose a reason for hiding this comment

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

This is very difficult to read, you can define a variable instead.

asv/console.py Outdated
progress_msg = '[{0:6.02f}%] '.format(
(float(self._count) / self._total) * 100.0)
percentage = (float(self._count) / self._total) * 100.0
progress_msg = f'[{percentage:6.02f}%] '
Copy link
Member

Choose a reason for hiding this comment

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

The original code wasn't great. In Python you can actually do this:

>>> value = .2
>>> f'{value:.2%}'
'20.00%'

Also, converting self._count to float only made sense in Python 2, in Python 3 it's not needed. So, this could be something like (make sure we're not changing the result):

progress_msg = f'[{self._count / self._total:6.02%}] '

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! thanks for the suggestion :)

@datapythonista datapythonista merged commit ac7189e into airspeed-velocity:master Mar 12, 2022
@datapythonista
Copy link
Member

Thanks @LucyJimenez, nice clean up

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.

2 participants