-
Notifications
You must be signed in to change notification settings - Fork 661
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
Fix "Prefer format()
over string interpolation operator" issue
#592
Conversation
This would have been one of the most tedious refactorings I could imagine... (especially as I never remember the exact format mini language). @MDAnalysis/coredevs , do you like this or is there a reason to stick with old interpolated strings? |
Cody is sometime overdoing it. Most of the time, it is not necessary to specify a format; |
Is the "overdoing" enough to not merge? |
The overdoing doesn't hurt. The overdoing ensures that everythings works exactly as before. So being a little bit over verbose is quite ok I'd say |
Need to check why XYZReader and friends fail with format strings. I think we can push to this branch autofix/wrapped2_to3_fix-3 to update the PR. |
Cody (quantifiedcode) uses exclamation marks in its format srtings. Some appear to cause error.
Remove some bangs from formated string
Why were these exclamation marks inserted by @quantifiedcode-bot ? The py 2.7 specs of the format mini language don't contain |
I did not know about the exclamation mark either. Yet, the doc does mention it:
|
Ah ! @kain88-de is faster than I am by 40 seconds ! |
@kain88-de --- thanks! Learnt something new .. such as RTFM...) Why did it fail in some cases, though? And more importantly: are there more such cases? @jbarnoud pointed out that the PR now passing does not mean that there aren't any other stinkers in this PR --- we don't cover many of the warning messages through the tests. A quick look at the diff showed that there were many more exclamation marks inserted. |
It might be a good idea to ask the people from QuantifiedCode. |
@kain88-de @orbeckst Happy to help. Can you explain what "failed in some cases"? |
@programmdesign First problematic changeThe first change goes from: logger.error("%4s %3d %3s %3s %6.3f | %4s %3d %3s %3s %6.3f" %
(ar.segid, ar.resid, ar.resname, ar.name, ar.mass,
at.segid, at.resid, at.resname, at.name, at.mass,)) to logger.error("{0:4!s} {1:3d} {2:3!s} {3:3!s} {4:6.3f} | {5:4!s} {6:3d} {7:3!s} {8:3!s} {9:6.3f}".format(ar.segid, ar.resid, ar.resname, ar.name, ar.mass,
at.segid, at.resid, at.resname, at.name, at.mass)) where The error is:
It is solved by removing the exclamation marks, which gives: logger.error("{0:4s} {1:3d} {2:3s} {3:3s} {4:6.3f} | {5:4s} {6:3d} {7:3s} {8:3s} {9:6.3f}".format(ar.segid, ar.resid, ar.resname, ar.name, ar.mass,
at.segid, at.resid, at.resname, at.name, at.mass)) Second problematic changeThe second change narrows the issue: self._xyz.write("%8s %10.5f %10.5f %10.5f\n" % (atom, x, y, z)) becomes self._xyz.write("{0:8!s} {1:10.5f} {2:10.5f} {3:10.5f}\n".format(atom, x, y, z)) which causes the same exception as for the first change and get solved in the same way. RationalI played a bit in iPython and it appears that In [13]: '%3s' % 'a'
Out[13]: ' a'
In [14]: '{0!s:3}'.format('a')
Out[14]: 'a '
In [15]: '{0:3s}'.format('a')
Out[15]: 'a ' The correct replacements for In [16]: '%3s' % 'a'
Out[16]: ' a'
In [17]: '{0!s:>3}'.format('a')
Out[17]: ' a'
In [18]: '{0:>3s}'.format('a')
Out[18]: ' a' So my quick fix does not actually fix the problem as the format output is not the expected one... Finally, and this is mostly for MDAnalysis devs as it is likely difficult to find automatically, the first change could be made even clearer with the format mini language: logger.error("{ar.segid:>4s} {ar.resid:3d} {ar.resname:>3s} {ar.name:>3s} {ar.mass:6.3f} | {at.segid:>4s} {at.resid:3d} {at.resname:>3s} {at.name:>3s} {at.mass:6.3f}".format(ar=ar, at=at)) |
@jbarnoud thanks for the detailed analysis. Just one question: in the first Cody example in First problematic change I don't see any exclamation marks. |
@orbeckst My bad, I copied the wrong version. I edited my comment accordingly. |
@jbarnoud , from what I understand, the real fix should look like Is it worthwhile continuing with this PR (and properly fixing all occurrences) or should we wait until we have better test coverage #597 and/or have taught cody the replacement that we would prefer? I am actually not to bothered about the exact formatting in output strings but when writing files with defined formats then we have to be careful. |
On 19/12/15 22:02, Oliver Beckstein wrote:
|
On 19 Dec, 2015, at 16:08, Jonathan Barnoud wrote:
Fixing them "the right way"?
As long as we know that the conversion will always give a string, I don't overly care about the exact alignment of the inserted string. However, anything that writes to a file is critical and would need to be checked, i.e. have a test case that passes before and after. Is it worthwhile? |
On 19/12/15 22:11, Oliver Beckstein wrote:
|
@jbarnoud any news on this? |
On 12/01/16 23:08, kain88-de wrote:
|
This pull request automatically fixes all occurrences of the following issue:
Issue type: Prefer
format()
over string interpolation operatorIssue details: https://www.quantifiedcode.com/app/project/gh:MDAnalysis:mdanalysis?groups=code_patterns/%3A4ACGxFj1
To adjust the commit message or the actual code changes, just rebase or cherry-pick the commits.
For questions or feedback reach out to [email protected].
Legal note: We won't claim any copyrights on the code changes.
Cheers,
Cody - Your code quality bot