-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
lib/matplotlib/cbook/__init__.py
Outdated
seta = mapping.pop(ref(a), None) | ||
if seta is not None: | ||
seta.remove(ref(a)) | ||
set_a = self._mapping.pop(ref(a)) |
There was a problem hiding this comment.
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.
lib/matplotlib/cbook/__init__.py
Outdated
|
||
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)])] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restored previous version
lib/matplotlib/cbook/__init__.py
Outdated
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()} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
both comments handled |
Mostly stylistic, except for the implementation of
__iter__
whichis 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