From c1ab6c6684e8bddfe0971e6b8d8cd2d66ab5acbe Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 19 Mar 2014 12:55:32 -0400 Subject: [PATCH 1/2] BUG : add __hash__ to AffineBase 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 --- lib/matplotlib/transforms.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 61e403a9103f..3b8cd1d893f8 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -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) + def transform(self, values): return self.transform_affine(values) transform.__doc__ = Transform.transform.__doc__ From c3bea7acef905429ef71136a1ecd37dcaf31fcf5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 19 Mar 2014 13:06:58 -0400 Subject: [PATCH 2/2] ENH : tweaked hash method and documentation --- lib/matplotlib/transforms.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 3b8cd1d893f8..d1baa15c9b41 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -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,12 +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 + # python3 requires that if a class defines __eq__ then in must + # define __hash__ if they need to be hashable (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) + return hash(self.get_matrix().tostring) def transform(self, values): return self.transform_affine(values)