Proper __hash__ and __eq__ for transforms.
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 20:21:05 +0000 (22:21 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 20:21:05 +0000 (22:21 +0200)
tabakrolletjie/loader.py
tabakrolletjie/transforms.py

index e4a5aef3de424ed8253f3f96d641d257bb4987d5..4ad1d585787be3cc944575c723708056db76eb8b 100644 (file)
@@ -40,14 +40,14 @@ class Loader(object):
         """Return a pygame surface of the requested image."""
         fn = self.full_path("images", *parts)
         transform = kwargs.pop("transform", self.NULL_TRANSFORM)
-        img = self._cache.get((fn, transform.hash), None)
+        img = self._cache.get((fn, transform), None)
         if img is None:
             img = pygame.image.load(fn)
             # We assume pygame.display has been initialised
             # Fix this if that changes
             img.convert_alpha(pygame.display.get_surface())
             img = transform.apply(img)
-            self._cache[(fn, transform.hash)] = img
+            self._cache[(fn, transform)] = img
         return img
 
     def load_font(self, *parts, **kwargs):
index 70ff036160618b51c2c5cd3651310172a23f6a36..c5eb05f64e7d85739910eb2f89e1e4f428095070 100644 (file)
@@ -11,11 +11,22 @@ class Transform(object):
         for k in self.ARGS:
             setattr(self, k, kw.pop(k))
         assert not kw
-        self.hash = "%s: <%s>" % (
+        self._repr = "%s: <%s>" % (
             self.__class__.__name__,
             ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
         return
 
+    def __repr__(self):
+        return self._repr
+
+    def __hash__(self):
+        return hash(self._repr)
+
+    def __eq__(self, other):
+        if not isinstance(other, Transform):
+            return NotImplemented
+        return self._repr == other._repr
+
     def apply(self, surface):
         raise NotImplementedError("Transforms should implement .apply")