comparison markup/template.py @ 70:dd73921530e8 trunk

Use `collections.deque` for the template context stack on Python 2.4, which improves performance if there are many context frame pop/push operations.
author cmlenz
date Tue, 11 Jul 2006 17:40:41 +0000
parents c40a5dcd2b55
children db7c76d337aa
comparison
equal deleted inserted replaced
69:c40a5dcd2b55 70:dd73921530e8
36 Random thoughts: 36 Random thoughts:
37 * Is there any need to support py:extends and/or py:layout? 37 * Is there any need to support py:extends and/or py:layout?
38 * Could we generate byte code from expressions? 38 * Could we generate byte code from expressions?
39 """ 39 """
40 40
41 try:
42 from collections import deque
43 except ImportError:
44 class deque(list):
45 def appendleft(self, x): self.insert(0, x)
46 def popleft(self): return self.pop(0)
41 import compiler 47 import compiler
42 import os 48 import os
43 import posixpath 49 import posixpath
44 import re 50 import re
45 from StringIO import StringIO 51 from StringIO import StringIO
115 >>> ctxt.get('one') 121 >>> ctxt.get('one')
116 'foo' 122 'foo'
117 """ 123 """
118 124
119 def __init__(self, **data): 125 def __init__(self, **data):
120 self.stack = [data] 126 self.frames = deque([data])
121
122 def __getitem__(self, key):
123 """Get a variable's value, starting at the current context frame and
124 going upward.
125 """
126 return self.get(key)
127 127
128 def __repr__(self): 128 def __repr__(self):
129 return repr(self.stack) 129 return repr(self.frames)
130 130
131 def __setitem__(self, key, value): 131 def __setitem__(self, key, value):
132 """Set a variable in the current context.""" 132 """Set a variable in the current context."""
133 self.stack[0][key] = value 133 self.frames[0][key] = value
134 134
135 def get(self, key): 135 def get(self, key):
136 """Get a variable's value, starting at the current context frame and 136 """Get a variable's value, starting at the current context frame and
137 going upward. 137 going upward.
138 """ 138 """
139 for frame in self.stack: 139 for frame in self.frames:
140 if key in frame: 140 if key in frame:
141 return frame[key] 141 return frame[key]
142 __getitem__ = get
142 143
143 def push(self, **data): 144 def push(self, **data):
144 """Push a new context frame on the stack.""" 145 """Push a new context frame on the stack."""
145 self.stack.insert(0, data) 146 self.frames.appendleft(data)
146 147
147 def pop(self): 148 def pop(self):
148 """Pop the top-most context frame from the stack. 149 """Pop the top-most context frame from the stack.
149 150
150 If the stack is empty, an `AssertionError` is raised. 151 If the stack is empty, an `AssertionError` is raised.
151 """ 152 """
152 assert self.stack, 'Pop from empty context stack' 153 #assert self.frames, 'Pop from empty context stack'
153 self.stack.pop(0) 154 self.frames.popleft()
154 155
155 156
156 class Directive(object): 157 class Directive(object):
157 """Abstract base class for template directives. 158 """Abstract base class for template directives.
158 159
Copyright (C) 2012-2017 Edgewall Software