Mercurial > genshi > genshi-test
diff markup/template.py @ 70:0498da8e5de7
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 | e9a3930f8823 |
children | 0334bca326df |
line wrap: on
line diff
--- a/markup/template.py +++ b/markup/template.py @@ -38,6 +38,12 @@ * Could we generate byte code from expressions? """ +try: + from collections import deque +except ImportError: + class deque(list): + def appendleft(self, x): self.insert(0, x) + def popleft(self): return self.pop(0) import compiler import os import posixpath @@ -117,40 +123,35 @@ """ def __init__(self, **data): - self.stack = [data] - - def __getitem__(self, key): - """Get a variable's value, starting at the current context frame and - going upward. - """ - return self.get(key) + self.frames = deque([data]) def __repr__(self): - return repr(self.stack) + return repr(self.frames) def __setitem__(self, key, value): """Set a variable in the current context.""" - self.stack[0][key] = value + self.frames[0][key] = value def get(self, key): """Get a variable's value, starting at the current context frame and going upward. """ - for frame in self.stack: + for frame in self.frames: if key in frame: return frame[key] + __getitem__ = get def push(self, **data): """Push a new context frame on the stack.""" - self.stack.insert(0, data) + self.frames.appendleft(data) def pop(self): """Pop the top-most context frame from the stack. If the stack is empty, an `AssertionError` is raised. """ - assert self.stack, 'Pop from empty context stack' - self.stack.pop(0) + #assert self.frames, 'Pop from empty context stack' + self.frames.popleft() class Directive(object):