Simplify get_legend_handler method #6639

Merged
merged 1 commit into from Jul 12, 2016

Conversation

Projects
None yet
3 participants
Member

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.

mdboom added the needs_review label Jun 24, 2016

Member

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?

Owner

tacaswell commented Jun 27, 2016

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 added this to the 2.1 (next point release) milestone Jun 27, 2016

Member

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
        ...

@tacaswell tacaswell commented on an outdated diff Jun 29, 2016

lib/matplotlib/legend.py
@@ -565,19 +565,23 @@ def get_legend_handler(legend_handler_map, orig_handle):
method-resolution-order. If no matching key is found, it
returns None.
"""
- legend_handler_keys = list(six.iterkeys(legend_handler_map))
- if orig_handle in legend_handler_keys:
- handler = legend_handler_map[orig_handle]
- else:
+ try:
+ hash(orig_handle)
+ except TypeError:
@tacaswell

tacaswell Jun 29, 2016

Owner

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.

@tacaswell tacaswell commented on an outdated diff Jul 3, 2016

lib/matplotlib/legend.py
@@ -565,19 +565,23 @@ def get_legend_handler(legend_handler_map, orig_handle):
method-resolution-order. If no matching key is found, it
returns None.
"""
- legend_handler_keys = list(six.iterkeys(legend_handler_map))
- if orig_handle in legend_handler_keys:
- handler = legend_handler_map[orig_handle]
+ try:
+ hash(orig_handle)
@tacaswell

tacaswell Jul 3, 2016

Owner

in cbook.py we have a ishashable function which basically does this, but returns a boolean. I can see going either way on using it here.

@Kojoley Kojoley Simplify get_legend_handler method
55adc49
Member

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

3 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.004%) to 70.277%
Details

tacaswell removed the needs_review label Jul 12, 2016

Kojoley deleted the Kojoley:simplify-get_legend_handler branch Jul 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment