Ticket #13812: 13812_faster_context.diff

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

     
    2121        return repr(self.dicts)
    2222
    2323    def __iter__(self):
    24         for d in reversed(self.dicts):
     24        for d in self.dicts:
    2525            yield d
    2626
    2727    def push(self):
    2828        d = {}
    29         self.dicts.append(d)
     29        self.dicts.insert(0, d)
    3030        return d
    3131
    3232    def pop(self):
    3333        if len(self.dicts) == 1:
    3434            raise ContextPopException
    35         return self.dicts.pop()
     35        return self.dicts.pop(0)
    3636
    3737    def __setitem__(self, key, value):
    3838        "Set a variable in the current context"
    39         self.dicts[-1][key] = value
     39        self.dicts[0][key] = value
    4040
    4141    def __getitem__(self, key):
    4242        "Get a variable's value, starting at the current context and going upward"
    43         for d in reversed(self.dicts):
     43        for d in self.dicts:
    4444            if key in d:
    4545                return d[key]
    4646        raise KeyError(key)
    4747
    4848    def __delitem__(self, key):
    4949        "Delete a variable from the current context"
    50         del self.dicts[-1][key]
     50        del self.dicts[0][key]
    5151
    5252    def has_key(self, key):
    5353        for d in self.dicts:
     
    5959        return self.has_key(key)
    6060
    6161    def get(self, key, otherwise=None):
    62         for d in reversed(self.dicts):
     62        for d in self.dicts:
    6363            if key in d:
    6464                return d[key]
    6565        return otherwise
     
    7676        "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
    7777        if not hasattr(other_dict, '__getitem__'):
    7878            raise TypeError('other_dict must be a mapping (dictionary-like) object.')
    79         self.dicts.append(other_dict)
     79        self.dicts.insert(0, other_dict)
    8080        return other_dict
    8181
    8282class RenderContext(BaseContext):
     
    9595    template context.
    9696    """
    9797    def __iter__(self):
    98         for d in self.dicts[-1]:
     98        for d in self.dicts[0]:
    9999            yield d
    100100
    101101    def has_key(self, key):
    102         return key in self.dicts[-1]
     102        return key in self.dicts[0]
    103103
    104104    def get(self, key, otherwise=None):
    105         d = self.dicts[-1]
     105        d = self.dicts[0]
    106106        if key in d:
    107107            return d[key]
    108108        return otherwise
Back to Top