Has anybody written a Memoize-like decorator for Zope?

I imagine that it could be a nice feature to have a standard decorator for Zope that would cache results in a _v_ volatile variable.

I usually do it a bit like this::

    def _cache_it(self, meth, *args, **kwargs):
        cache_name = '_v_%s' % meth.__name__
        if not hasattr(self, cache_name):
            result = meth(*args, **kwargs)
            setattr(self, cache_name, result)
        return getattr(self, cache_name)

    def _some_function(self):
        # do expensive calculations and return result
        return 42

    def some_function(self, *args, **kwargs):
        "Returns cached values"
        return self._cache_it(self._some_function, *args, **kwargs)

But I guess that there must be someone who has allready written a more generic way to do it via generators?


hilsen/regards Max M, Denmark

IT's Mad Science

Zope maillist  -  Zope@zope.org
**   No cross posts or HTML encoding!  **
(Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )

Reply via email to