Ticket #6217: django-lazy-piglet.diff

File django-lazy-piglet.diff, 3.6 KB (added by Jakub Wilk <ubanus@…>, 17 years ago)
  • functional.py

     
    3737    """
    3838    class __proxy__(Promise):
    3939        # 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.
    4342        def __init__(self, args, kw):
    4443            self.__func = func
    4544            self.__args = args
    4645            self.__kw = kw
    47             self.__dispatch = {}
     46
     47        @classmethod
     48        def __class_init__(cls):
     49            cls.__dispatch = {}
    4850            for resultclass in resultclasses:
    49                 self.__dispatch[resultclass] = {}
     51                cls.__dispatch[resultclass] = {}
    5052                for (k, v) in resultclass.__dict__.items():
    51                     setattr(self, k, self.__promise__(resultclass, k, v))
    52             self._delegate_str = str in resultclasses
    53             self._delegate_unicode = unicode in resultclasses
    54             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 this
    57                 # doesn't interfere with any other lazy() results.
    58                 __proxy__.__unicode__ = __proxy__.__unicode_cast
    59             elif self._delegate_str:
    60                 __proxy__.__str__ = __proxy__.__str_cast
     53                    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
    6163
    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):
    6669                # Automatically triggers the evaluation of a lazy value and
    6770                # applies the given magic method of the result type.
    6871                res = self.__func(*self.__args, **self.__kw)
    6972                return self.__dispatch[type(res)][funcname](res, *args, **kw)
    7073
    71             if klass not in self.__dispatch:
    72                 self.__dispatch[klass] = {}
    73             self.__dispatch[klass][funcname] = func
     74            if resultclass not in cls.__dispatch:
     75                cls.__dispatch[resultclass] = {}
     76            cls.__dispatch[resultclass][funcname] = func
    7477            return __wrapper__
    7578
    7679        def __unicode_cast(self):
     
    105108            # complicated for copying.
    106109            memo[id(self)] = self
    107110            return self
     111   
     112    __proxy__.__class_init__()
    108113
    109114    def __wrapper__(*args, **kw):
    110115        # Creates the proxy object, instead of the actual value.
Back to Top