diff 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
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):
Copyright (C) 2012-2017 Edgewall Software