Ticket #13812: 13812_faster_context_using_deque.diff

File 13812_faster_context_using_deque.diff, 2.6 KB (added by Satoru Logic, 14 years ago)
  • context.py

     
     1import collections
     2
    13from django.core.exceptions import ImproperlyConfigured
    24from django.utils.importlib import import_module
    35
     
    1517class BaseContext(object):
    1618    def __init__(self, dict_=None):
    1719        dict_ = dict_ or {}
    18         self.dicts = [dict_]
     20        self.dicts = collections.deque((dict_,))
    1921
    2022    def __repr__(self):
    2123        return repr(self.dicts)
    2224
    2325    def __iter__(self):
    24         for d in reversed(self.dicts):
     26        for d in self.dicts:
    2527            yield d
    2628
    2729    def push(self):
    2830        d = {}
    29         self.dicts.append(d)
     31        self.dicts.appendleft(d)
    3032        return d
    3133
    3234    def pop(self):
    3335        if len(self.dicts) == 1:
    3436            raise ContextPopException
    35         return self.dicts.pop()
     37        return self.dicts.popleft()
    3638
    3739    def __setitem__(self, key, value):
    3840        "Set a variable in the current context"
    39         self.dicts[-1][key] = value
     41        self.dicts[0][key] = value
    4042
    4143    def __getitem__(self, key):
    4244        "Get a variable's value, starting at the current context and going upward"
    43         for d in reversed(self.dicts):
     45        for d in self.dicts:
    4446            if key in d:
    4547                return d[key]
    4648        raise KeyError(key)
    4749
    4850    def __delitem__(self, key):
    4951        "Delete a variable from the current context"
    50         del self.dicts[-1][key]
     52        del self.dicts[0][key]
    5153
    5254    def has_key(self, key):
    5355        for d in self.dicts:
     
    5961        return self.has_key(key)
    6062
    6163    def get(self, key, otherwise=None):
    62         for d in reversed(self.dicts):
     64        for d in self.dicts:
    6365            if key in d:
    6466                return d[key]
    6567        return otherwise
     
    7678        "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
    7779        if not hasattr(other_dict, '__getitem__'):
    7880            raise TypeError('other_dict must be a mapping (dictionary-like) object.')
    79         self.dicts.append(other_dict)
     81        self.dicts.appendleft(other_dict)
    8082        return other_dict
    8183
    8284class RenderContext(BaseContext):
     
    9597    template context.
    9698    """
    9799    def __iter__(self):
    98         for d in self.dicts[-1]:
     100        for d in self.dicts[0]:
    99101            yield d
    100102
    101103    def has_key(self, key):
    102         return key in self.dicts[-1]
     104        return key in self.dicts[0]
    103105
    104106    def get(self, key, otherwise=None):
    105         d = self.dicts[-1]
     107        d = self.dicts[0]
    106108        if key in d:
    107109            return d[key]
    108110        return otherwise
Back to Top