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

Simplify get_legend_handler method #6639

Merged
merged 1 commit into from Jul 12, 2016

Conversation

Kojoley
Copy link
Member

@Kojoley Kojoley commented Jun 24, 2016

I have simplified the get_legend_handler method. There is no need to create list from keys of legend_handler_map and no need in two dictionary lookups (if key in dict: return dict[key]).
Loop can be optimized even more, but will result in a code bloat, which does not worth of saving few cpu cycles.

@Kojoley
Copy link
Member Author

Kojoley commented Jun 24, 2016

It is not clear for me why there was no check for unhashble orig_handle in original method as if it is then it cannot be a key of a dictionary and maybe lines below was some unclear way to do such check?

        legend_handler_keys = list(six.iterkeys(legend_handler_map))
        if orig_handle in legend_handler_keys:

Should I add such check or close pull request?

@tacaswell
Copy link
Member

It is side-stepping having to do a check by using a simpler data structure. The __contains__ check on a list is just a linear search (so hashability does not matter). If it is is the list, then it is a key (and hence must be hashable), if the key is not hashible, it can not be a key and hence will not be in the list.

I would revert the first set of changes, add a comment as to why it is that way, and leave the second half.

Adding a test to the test suite that exercises the pathological input would be good too.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 27, 2016
@Kojoley
Copy link
Member Author

Kojoley commented Jun 27, 2016

Yes, I understand this, but here we have a linear search in a dictionary by creating temporary list of dictionary keys and this list lives only withing the function.
As we cannot use isinstance(orig_handle, collections.Hashable) (because some objects define itself as hashable, but can become unhashable in runtime e.g. tuple) here I think check below is more suitable than the linear search.

    def get_legend_handler(legend_handler_map, orig_handle):
        try:
            hash(orig_handle)
        except TypeError:
            return None
        ...

else:
try:
hash(orig_handle)
except TypeError:
Copy link
Member

Choose a reason for hiding this comment

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

This is different behavior, If it is not hash-able, then we need to walk it's MRO to see if we have a handle for any of it's classes.

This is apparently not tested well enough.

@Kojoley Kojoley force-pushed the simplify-get_legend_handler branch from 7d610d1 to 55adc49 Compare July 3, 2016 14:51
@Kojoley
Copy link
Member Author

Kojoley commented Jul 3, 2016

I have updated PR as per your suggestion and squashed multiple commits

@tacaswell tacaswell merged commit baf812c into matplotlib:master Jul 12, 2016
@Kojoley Kojoley deleted the simplify-get_legend_handler branch July 12, 2016 10:29
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.

None yet

3 participants