From: Simon Cross Date: Fri, 9 Sep 2016 20:21:05 +0000 (+0200) Subject: Proper __hash__ and __eq__ for transforms. X-Git-Tag: tabakrolletjie-v1.0.0~153 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=68bffb19c42c6b325c75f11b83405701ce9ea761;p=tabakrolletjie.git Proper __hash__ and __eq__ for transforms. --- diff --git a/tabakrolletjie/loader.py b/tabakrolletjie/loader.py index e4a5aef..4ad1d58 100644 --- a/tabakrolletjie/loader.py +++ b/tabakrolletjie/loader.py @@ -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): diff --git a/tabakrolletjie/transforms.py b/tabakrolletjie/transforms.py index 70ff036..c5eb05f 100644 --- a/tabakrolletjie/transforms.py +++ b/tabakrolletjie/transforms.py @@ -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")