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

Issue 592 format redo #623

Merged
merged 2 commits into from
Jan 15, 2016

Conversation

jbarnoud
Copy link
Contributor

This PR reapply Cody/QC fix that replaces string replacement using '%' by string replacement using the format method to the current develop version. It replaces PR #592.

Cody/QC suggested a fix to replace all string replacements using % by
string replacements using the format method. Yet, the suggested
replacement introduce a syntax error by replacing all occurences similar
to %4s to something like {0:4!s}; where what follows the column is
the format for the replacement and what follows the bang is the type for
the conversion.

Cody's replacement leads to the following exception:

ValueError: Invalid conversion specification

It appears that the column part and the bang part of the format should
be written the other way around so {0:4!s} should actually be
{0!s>4}. Note that the > character is important as it describes how
the data should be aligned. By default, without the >, strings are flush
to the left while %4s flush strings to the right. Note that {0!d:4}
does flush to the right on the contrary to what happens with strings.

In MDAnalysis, the only faulty replacements where about string format.
They could be detected using the following grep statement:

egrep -Ro '\{[^:}]*:[^!}]*![^}]*\}' *

I fixed the faulty replacement with the following command:

find ./ -name '*.py' -exec \
    sed -rie 's/\{([^:}]*):([^!}]*)!s\}/{\1!s:>\2}/g' {} \;

quantifiedcode-bot and others added 2 commits January 15, 2016 18:55
Cody/QC suggested a fix to replace all string replacements using % by
string replacements using the `format` method. Yet, the suggested
replacement introduce a syntax error by replacing all occurences similar
to '%4s' to something like '{0:4!s}'; where what follows the column is
the format for the replacement and what follows the bang is the type for
the conversion.

Cody's replacement leads to the following exception:

    ValueError: Invalid conversion specification

It appears that the column part and the bang part of the format should
be written the other way around so '{0:4!s}' should actually be
'{0!s>4}'. Note that the > character is important as it describes how
the data should be aligned. By default, without the >, strings are flush
to the left while '%4s' flush strings to the right. Note that '{0!d:4}'
does flush to the right on the contrary to what happens with strings.

In MDAnalysis, the only faulty replacements where about string format.
They could be detected using the following grep statement:

    egrep -Ro '\{[^:}]*:[^!}]*![^}]*\}' *

I fixed the faulty replacement with the following command:

    find ./ -name '*.py' -exec \
        sed -rie 's/\{([^:}]*):([^!}]*)!s\}/{\1!s:>\2}/g' {} \;
@richardjgowers
Copy link
Member

find ./ -name '*.py' -exec sed -rie 's/\{([^:}]*):([^!}]*)!s\}/{\1!s:>\2}/g' {} \;
It seems so obvious now! :P

richardjgowers added a commit that referenced this pull request Jan 15, 2016
@richardjgowers richardjgowers merged commit 5044e90 into MDAnalysis:develop Jan 15, 2016
@richardjgowers
Copy link
Member

Thanks, didn't look like a fun one

@jbarnoud jbarnoud deleted the Issue-592-format-redo branch January 15, 2016 20:38
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