Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify the grouper implementation. #10958

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
merged 1 commit into from
Apr 5, 2018
Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 4, 2018

Mostly stylistic, except for the implementation of __iter__ which
is made much shorter: to get list of unique lists that appear in
self._mapping.values(), it is simpler to construct a dict keying these
lists based on their id()s rather than appending a marker on the lists
and then popping the markers at the end.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added this to the v3.0 milestone Apr 4, 2018
seta = mapping.pop(ref(a), None)
if seta is not None:
seta.remove(ref(a))
set_a = self._mapping.pop(ref(a))
Copy link
Member

Choose a reason for hiding this comment

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

I think you still need the second argument, None. Otherwise you will risk a KeyError.


siblings = self._mapping.get(ref(a), [ref(a)])
return [x() for x in siblings]
return [x() for x in self._mapping.get(ref(a), [ref(a)])]
Copy link
Member

Choose a reason for hiding this comment

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

I think it helps if code is a bit self-documenting. I prefer the way it was before.

Copy link
Member

Choose a reason for hiding this comment

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

I agree; I like concise code, but shorter is not always better, and this is a great example of that. That intermediate variable works wonders for readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

restored previous version

for group in self._mapping.values():
if group[-1] is token:
del group[-1]
for group in ({id(group): group for group in self._mapping.values()}
Copy link
Member

Choose a reason for hiding this comment

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

I can understand what the other code is doing, though I agree it looks inelegant. This change is maybe more elegant, but its not very clear whats happening. Can you add a line of explanation for the folks who don't parse more obscure python constructs?

Copy link
Member

Choose a reason for hiding this comment

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

An additional variable may help:

unique_groups = {id(group): group for group in self._mapping.values()}.values()
for group in unique_groups:

Copy link
Member

Choose a reason for hiding this comment

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

👍 Then I don't really need to understand the magic to see whats going on.

Sorry to admit my python illiteracy...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds fair, fixed

Mostly stylistic, except for the implementation of `__iter__` which
is made much shorter: to get list of unique lists that appear in
self._mapping.values(), it is simpler to construct a dict keying these
lists based on their id()s rather than appending a marker on the lists
and then popping the markers at the end.
@anntzer
Copy link
Contributor Author

anntzer commented Apr 5, 2018

both comments handled

@jklymak jklymak merged commit 7ae9062 into matplotlib:master Apr 5, 2018
@anntzer anntzer deleted the grouper branch April 5, 2018 21:48
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.

4 participants