Skip to content

ENH: EngFormatter new kwarg 'sep' #6542

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

Merged

Conversation

afvincent
Copy link
Contributor

Implements the proposition from #6533, by adding a kwarg space_sep to ticker.EngFormatter.

The default value of this new kwarg space_sep is True which preserves the current formatting, with a space separator between the value and the prefix/unit (like in "1.23 µV"). If it is set to False, this space separator is removed (like in "1.23µV"). The latter is not really correct from the typographic viewpoint but will (for example) allow users who don't really care to save space in the ticklabels. As discussed in #6533, when no unit symbol is provided, "1.23 µ" and "1.23µ" seem to be both wrong (at least for typographers), so I chose to keep the current behavior as default.

The commit 0f37c41 fixes a minor inconsistency between different representations of zero. Now, formatter(-0.0) == formatter(0) == formatter(-0) == u"0" (with formatter = EngFormatter()), while before formatter(-0) = u"0" but formatter(-0.0) = u"-0" for example...

The commit 25cdf5f is a rewrite of the unit test test_EngFormatter_formatting() I previously added in PR #6014 . The new test now tests all the possible combinations of the kwargs, which was not the case before (in particular, only the default places=None was tested). Furthermore, the tested values now exercise a bigger range of cases (like for example 0, -0 and -0.0).

And by the way, I tried to improve a few docstrings. Hopefully I didn't add to many English mistakes or typos...

@afvincent afvincent changed the title Enh engformatter space sep new option ENH: engformatter space sep new option Jun 6, 2016
@afvincent afvincent changed the title ENH: engformatter space sep new option ENH: EngFormatter space_sep new option Jun 6, 2016
@afvincent
Copy link
Contributor Author

Note to myself: don't forget to update the example once some review will have been performed.

elif self.places > 0:
format_str = ("%%.%if %%s" % self.places)
format_str = "%.{p:d}f{sep:s}%s".format(p=self.places,
sep=self.sep)

formatted = format_str % (mant, prefix)

formatted = formatted.strip()
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this strip

Copy link
Member

Choose a reason for hiding this comment

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

And move it up to L1088 (in the new code)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the solution in commit 2193c79 is more or less what you were proposing.

In fact, the strip at the end of format_eng was the last operation done before the fix #6014, which added the if statement that appends a trailing space when there was no prefix and unit (to fix issue #6009). It seems to me that with the new space_sep option, this if block is not necessary anymore and that one simply needs to perform a strip in __call__ before returning the final formatted string: the only case where it will remove a trailing space should the one without a unit and without a prefix. The relevant test seems still OK with the new scheme.

By the way, since #6014 (and only if space_sep=True since #6542), format_eng(0) returns u"0 " and not u"0" as it is written in the docstring. So, a) is this change an issue, since format_eng is not a private method?, b) should I fix the docstring?

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 6, 2016
"""
eng_formatter = mticker.EngFormatter(places=places, space_sep=False)
# Remove the space separator from the reference cases
test_cases = ((val, u"{s:s}".format(s=s.replace(" ", "")))
Copy link
Member

Choose a reason for hiding this comment

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

Is the string format really needed here?

Copy link
Contributor Author

@afvincent afvincent Jun 7, 2016

Choose a reason for hiding this comment

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

I simply copy-pasted from one of the previous similar cases and didn't pay more attention, but you are right: it's clearer without the string format. Removed by commit 8d086e0.

@afvincent
Copy link
Contributor Author

Not sure the Travis failure is related to my last commit.

@afvincent
Copy link
Contributor Author

BTW, what should be done about the problem noticed l. 1134 in ticker.py (# TODO: shouldn't we raise a warning if self.places < 0?)? Should an explicite exception be raised, or do we assume users won't call an instance of EngFormatter with a negative value for the kwarg places.

@afvincent
Copy link
Contributor Author

I rewrote the example about the EngFormatter in the API section (engineering_formatter.py) to demonstrate about the new option space_sep. Eventhough most of the lines have been modified, it is the same spirit and data as before (the former example is kept as left panel).

I simply moved the demo of the kwarg places from the former example (left panel) to the new one (right panel) for esthetical reasons: with places=1 in the left panel, the tick labels were a bit mixed.

Here is a example how it now looks like:
updated_example

Copy link
Member

@phobson phobson left a comment

Choose a reason for hiding this comment

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

@QuLogic
Do the tests for this need to be re-written for pytest?

@QuLogic
Copy link
Member

QuLogic commented Feb 2, 2017

Yes, the nose stuff needs to be removed now.

@afvincent afvincent self-assigned this Feb 10, 2017
@afvincent
Copy link
Contributor Author

I will try to update to pytest tomorrow (“later today” may be more correct).

@afvincent afvincent force-pushed the enh_engformatter_space_sep_new_option branch from aa8cbd5 to e1421c6 Compare February 11, 2017 12:22
@afvincent
Copy link
Contributor Author

I rebased my local copy on the current master (things did not go totally smooth). I removed the changes that I had made in test_ticker because they were conflicting with the ones made during the Pytest migration. And of course, I have issues with Pytest when I want to check if I broke anything (it is complaining about cycler, which is installed and up-to-date on my system though).

So I will wait for CI to finish running on the rebased version before re-adding my previous changes in test_ticker, converted to Pytest.

@afvincent
Copy link
Contributor Author

Huh, what did I do wrong with rebase to get twice my GitHub thumbnail with commits 43d812d, 9121205, a1a38c5 and 7bad242 ^^?

@dopplershift
Copy link
Contributor

I wouldn't call it wrong, but I bet you have a new email set on github; the second icon is for the person who committed the change, rather than the author.

@afvincent
Copy link
Contributor Author

As Travis seems to be fine with the rebase (and the AppVeyor failure does not look like related), I have just (re-)included to extended test of EngFormatter that I had introduced before (If I did not forget any, it should of course include all the test cases that were tested in the previous commit). I hope I translated it well in Pytest style: we will see if CI (at least Travis) is still happy with this new version!

@dopplershift I think you may be write for the double thumbnail (I remind pushing stuff from computers with different email addresses). Well I guess that in the end, we will play with the mailmap file if it does mess with the statistics.

@afvincent
Copy link
Contributor Author

Well, at least both CI services agree on failing \o/… From a rapid glance at the logs, it seems that I mixed some “expected outputs” in the test revamping. I will double check and take care of it.

@Tillsten
Copy link
Contributor

Why not use a half space or make the space character customizable?

@afvincent
Copy link
Contributor Author

Well I think it just seemed easier to me to implement this with a boolean flag when I opened this PR (I was more or less a newcomer to mpl/git/test suite & Co).

What you suggest may indeed be better from the user viewpoint. We could replace the kwarg space_sep=True(default)|False with separator=u" " (default)|any string. Because backward compatibility is something we worship, the new default would still have to be a full space (not even unbreakable :/) as it is the original behavior. However, you could remove the space separator with separator="" or even use a narrow no-break white space with separator=u'\u202f' if you prefer.

@tacaswell @QuLogic @phobson @dopplershift (summonning every people who posted in the thread ^^) Thoughts? Who this idea be better than the current one?

@afvincent
Copy link
Contributor Author

BTW, just sep may be a better name for a customizable separator argument. For example, I just discovered that print also has a separator argument, called sep. (Always learning…)

@tacaswell
Copy link
Member

👍 to sep as the kwarg name and 👍 to it being configurable rather than just a boolean.

👎 to a non-ascii default, but 👍 to mentioning that in the docs.

@Tillsten
Copy link
Contributor

What wrong with an non ASCII default?

@afvincent
Copy link
Contributor Author

@tacaswell Just to be sure, by non-ascii default, you mean that it should be sep=" " instead of sep=u" "?

@tacaswell
Copy link
Member

I mean " " instead of '\u202f'.

You should not need the leading u anywhere. We have unicode_literals at the top of all of our files. We no longer support 3.2 so they are not forbidden anymore, but just not needed.

@afvincent afvincent force-pushed the enh_engformatter_space_sep_new_option branch from 033111f to ac42b94 Compare August 25, 2017 16:40
@afvincent
Copy link
Contributor Author

@WeatherGod I just rebased and dropped the last commit, which introduced the use of a regex.

@HDembinski Thank you for benchmarking :).

@afvincent
Copy link
Contributor Author

I do not understand what is going wrong with AppVeyor :/. I already restarted it twice and it always fails at during the same build (Python 3.5) with what look like network issues

CondaError: CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.continuum.io/pkgs/free/win-64/mkl-2017.0.3-0.tar.bz2>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

But three times in a row, it is not really intermittent to me anymore ^^...

@afvincent afvincent added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Aug 26, 2017
@afvincent
Copy link
Contributor Author

Marking it as release-critical because it is a feature that I would really appreciate to see in next release and I think it is finally in good shape (apart from the AppVeyor failure maybe). Feel free to remove the label if you disagree.

@tacaswell tacaswell merged commit df6acf9 into matplotlib:master Aug 26, 2017
@tacaswell
Copy link
Member

🎉

@afvincent
Copy link
Contributor Author

@tacaswell Thank you :).

And many thanks to all the people who got involved and reviewed this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants