Ticket #6217: django-lazy-piglet.diff
File django-lazy-piglet.diff, 3.6 KB (added by , 17 years ago) |
---|
-
functional.py
37 37 """ 38 38 class __proxy__(Promise): 39 39 # This inner class encapsulates the code that should be evaluated 40 # lazily. On calling of one of the magic methods it will force 41 # the evaluation and store the result. Afterwards, the result 42 # is delivered directly. So the result is memoized. 40 # lazily. On calling of one of the methods it will force the 41 # evaluation. 43 42 def __init__(self, args, kw): 44 43 self.__func = func 45 44 self.__args = args 46 45 self.__kw = kw 47 self.__dispatch = {} 46 47 @classmethod 48 def __class_init__(cls): 49 cls.__dispatch = {} 48 50 for resultclass in resultclasses: 49 self.__dispatch[resultclass] = {}51 cls.__dispatch[resultclass] = {} 50 52 for (k, v) in resultclass.__dict__.items(): 51 setattr(self, k, self.__promise__(resultclass, k, v))52 self._delegate_str = str in resultclasses53 self._delegate_unicode = unicode in resultclasses54 assert not (self._delegate_str and self._delegate_unicode), "Cannot call lazy() with both str and unicode return types."55 if self._delegate_unicode:56 # Each call to lazy() makes a new __proxy__ object, so this57 # doesn't interfere with any other lazy() results.58 __proxy__.__unicode__ = __proxy__.__unicode_cast59 elif self._delegate_str:60 __proxy__.__str__ = __proxy__.__str_cast53 if hasattr(cls, k): 54 continue 55 setattr(cls, k, cls.__promise__(resultclass, k, v)) 56 cls._delegate_str = str in resultclasses 57 cls._delegate_unicode = unicode in resultclasses 58 assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types." 59 if cls._delegate_unicode: 60 cls.__unicode__ = cls.__unicode_cast 61 elif cls._delegate_str: 62 cls.__str__ = cls.__str_cast 61 63 62 def __promise__(self, klass, funcname, func): 63 # Builds a wrapper around some magic method and registers that magic 64 # method for the given type and method name. 65 def __wrapper__(*args, **kw): 64 @classmethod 65 def __promise__(cls, resultclass, funcname, func): 66 # Builds a wrapper around a method and registers that method for 67 # the given type and method name. 68 def __wrapper__(self, *args, **kw): 66 69 # Automatically triggers the evaluation of a lazy value and 67 70 # applies the given magic method of the result type. 68 71 res = self.__func(*self.__args, **self.__kw) 69 72 return self.__dispatch[type(res)][funcname](res, *args, **kw) 70 73 71 if klass not in self.__dispatch:72 self.__dispatch[klass] = {}73 self.__dispatch[klass][funcname] = func74 if resultclass not in cls.__dispatch: 75 cls.__dispatch[resultclass] = {} 76 cls.__dispatch[resultclass][funcname] = func 74 77 return __wrapper__ 75 78 76 79 def __unicode_cast(self): … … 105 108 # complicated for copying. 106 109 memo[id(self)] = self 107 110 return self 111 112 __proxy__.__class_init__() 108 113 109 114 def __wrapper__(*args, **kw): 110 115 # Creates the proxy object, instead of the actual value.