Ticket #13812: 13812_faster_context.diff
File 13812_faster_context.diff, 2.3 KB (added by , 14 years ago) |
---|
-
context.py
21 21 return repr(self.dicts) 22 22 23 23 def __iter__(self): 24 for d in reversed(self.dicts):24 for d in self.dicts: 25 25 yield d 26 26 27 27 def push(self): 28 28 d = {} 29 self.dicts. append(d)29 self.dicts.insert(0, d) 30 30 return d 31 31 32 32 def pop(self): 33 33 if len(self.dicts) == 1: 34 34 raise ContextPopException 35 return self.dicts.pop( )35 return self.dicts.pop(0) 36 36 37 37 def __setitem__(self, key, value): 38 38 "Set a variable in the current context" 39 self.dicts[ -1][key] = value39 self.dicts[0][key] = value 40 40 41 41 def __getitem__(self, key): 42 42 "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: 44 44 if key in d: 45 45 return d[key] 46 46 raise KeyError(key) 47 47 48 48 def __delitem__(self, key): 49 49 "Delete a variable from the current context" 50 del self.dicts[ -1][key]50 del self.dicts[0][key] 51 51 52 52 def has_key(self, key): 53 53 for d in self.dicts: … … 59 59 return self.has_key(key) 60 60 61 61 def get(self, key, otherwise=None): 62 for d in reversed(self.dicts):62 for d in self.dicts: 63 63 if key in d: 64 64 return d[key] 65 65 return otherwise … … 76 76 "Like dict.update(). Pushes an entire dictionary's keys and values onto the context." 77 77 if not hasattr(other_dict, '__getitem__'): 78 78 raise TypeError('other_dict must be a mapping (dictionary-like) object.') 79 self.dicts. append(other_dict)79 self.dicts.insert(0, other_dict) 80 80 return other_dict 81 81 82 82 class RenderContext(BaseContext): … … 95 95 template context. 96 96 """ 97 97 def __iter__(self): 98 for d in self.dicts[ -1]:98 for d in self.dicts[0]: 99 99 yield d 100 100 101 101 def has_key(self, key): 102 return key in self.dicts[ -1]102 return key in self.dicts[0] 103 103 104 104 def get(self, key, otherwise=None): 105 d = self.dicts[ -1]105 d = self.dicts[0] 106 106 if key in d: 107 107 return d[key] 108 108 return otherwise