From 042bdcefb61e47ebb37897c64ffd86d47f43a3fd Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Wed, 5 Dec 2012 23:38:10 -0500 Subject: [PATCH] Simplify dynamic spec definitions Use a closure instead of an attribute to store the class attrs. --- imagekit/specs/__init__.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/imagekit/specs/__init__.py b/imagekit/specs/__init__.py index 9db70d0..df99721 100644 --- a/imagekit/specs/__init__.py +++ b/imagekit/specs/__init__.py @@ -144,9 +144,14 @@ class ImageSpec(BaseImageSpec): def create_spec_class(class_attrs): - cls = type('Spec', (DynamicSpec,), class_attrs) - cls._spec_attrs = class_attrs - return cls + + class DynamicSpecBase(ImageSpec): + def __reduce__(self): + kwargs = dict(self.kwargs) + kwargs['source_file'] = self.source_file + return (create_spec, (class_attrs, kwargs)) + + return type('DynamicSpec', (DynamicSpecBase,), class_attrs) def create_spec(class_attrs, kwargs): @@ -154,13 +159,6 @@ def create_spec(class_attrs, kwargs): return cls(**kwargs) -class DynamicSpec(ImageSpec): - def __reduce__(self): - kwargs = dict(self.kwargs) - kwargs['source_file'] = self.source_file - return (create_spec, (self._spec_attrs, kwargs)) - - class SpecHost(object): """ An object that ostensibly has a spec attribute but really delegates to the