-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
BUG : add __hash__ to AffineBase #2909
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
python3 requires that if a class defines __eq__ then in must define __hash__ (so that if a == b then hash(a) == hash(b)). Define 64bit hash by pulling the first 8 characters out of a sha1 hash of the matrix closes #2828
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
unicode_literals) | ||
|
||
import six | ||
|
||
import hashlib | ||
import numpy as np | ||
from numpy import ma | ||
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox, | ||
|
@@ -1570,6 +1570,13 @@ def __eq__(self, other): | |
return np.all(self.get_matrix() == other.get_matrix()) | ||
return NotImplemented | ||
|
||
# python3 requires that if a class defines __eq__ then in must define | ||
# __hash__ (so that if a == b then hash(a) == hash(b)). Define 64bit | ||
# hash by pulling the first 8 characters out of a sha1 hash of the matrix | ||
# see issue #2828 | ||
def __hash__(self): | ||
return int(hashlib.sha1(self.get_matrix()).hexdigest()[:8], 16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a correct approach. But I wonder if it's faster to do:
(Don't know if it is, just though the code would spend more time in C that way...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed
|
||
|
||
def transform(self, values): | ||
return self.transform_affine(values) | ||
transform.__doc__ = Transform.transform.__doc__ | ||
|
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.
Where is that stated? The documentation states that it is perfectly valid for objects to define
__eq__
and not__hash__
(http://docs.python.org/3.4/reference/datamodel.html#object.__hash__) as far as I can see.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.
They must if they want to be hashable, clarified the comment.
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.
Why do transforms want to be hashable? They aren't currently AFAICT?