import time

# From http://code.activestate.com/recipes/325905/
# with some modifications (added parametrization of the cache)
class MWT(object):
    """Memoize With Timeout"""
    _caches = {}
    _timeouts = {}

    def __init__(self, timeout=2, cache_attr_name = None):
        self.timeout = timeout
        self.cache_attr_name = cache_attr_name

    def collect(self):
        """Clear cache of results which have timed out"""
        for func in self._caches:
            cache = {}
            for key in self._caches[func]:
                if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
                    cache[key] = self._caches[func][key]
            self._caches[func].clear()
            self._caches[func].update(cache)

    def __call__(self, f):
        self._timeouts[f] = self.timeout

        def func(*args, **kwargs):

            # idea sort of from http://pypi.python.org/pypi/gocept.cache but merged back into MWT
            if (self.cache_attr_name == None):
              self.cache = self._caches[f] = {}
            else:
              try:
                the_self = args[0]
                self.cache = getattr(the_self, self.cache_attr_name)
                self._caches[f] = self.cache
              except (IndexError, AttributeError):
                raise TypeError(
                    "MWT could not retrieve cache attribute '%s' for function %r"
                    % (self.cache_attr_name, f.func_name))


            kw = kwargs.items()
            kw.sort()
            key = (args, tuple(kw))
            try:
                v = self.cache[key]
#                print "cache hit for " + str(id(self.cache))
                if (time.time() - v[1]) > self.timeout:
                    raise KeyError
            except KeyError:
#                print "new for " + str(id(self.cache))
                v = self.cache[key] = f(*args,**kwargs),time.time()
            return v[0]
        func.func_name = f.func_name

        return func


@@ -339,24 +339,50 @@
 he_attr_name
+%0A        self.cache = None
 %0A%0A    def co
@@ -792,95 +792,34 @@
 def 
-__call__(self, f):%0A        self._timeouts%5Bf%5D = self.timeout%0A%0A        def func(*
+init_cache(self, f, 
 args, 
-**
 kwar
@@ -823,26 +823,24 @@
 wargs):%0A
-%0A 
         
    # ide
@@ -831,19 +831,16 @@
         
-   
 # idea s
@@ -921,28 +921,24 @@
 MWT%0A        
-    
 if (self.cac
@@ -962,36 +962,32 @@
 one):%0A          
-   
- 
 self.cache = sel
@@ -1012,20 +1012,16 @@
         
-    
 else:%0A  
@@ -1020,36 +1020,32 @@
 else:%0A          
-    
 try:%0A           
@@ -1045,20 +1045,16 @@
         
- 
-   
 the_self
@@ -1060,28 +1060,24 @@
 f = args%5B0%5D%0A
-    
             
@@ -1133,36 +1133,32 @@
 me)%0A            
-   
- 
 self._caches%5Bf%5D 
@@ -1170,20 +1170,16 @@
 f.cache%0A
-    
         
@@ -1217,20 +1217,16 @@
 Error):%0A
-    
         
@@ -1262,20 +1262,16 @@
         
-    
 %22MWT cou
@@ -1340,20 +1340,16 @@
         
-    
 %25 (self.
@@ -1381,16 +1381,204 @@
 ame))%0A%0A%0A
+    def __call__(self, f):%0A        self._timeouts%5Bf%5D = self.timeout%0A%0A        def func(*args, **kwargs):%0A%0A            if self.cache == None:%0A              self.init_cache(f, args, kwargs)%0A%0A
         

Make sure we don't initialize the cache instance each time