[Zope-CMF] Re: RFC: browser views and memoization

Vladimir Iliev vladimir.iliev at gmail.com
Mon Jan 16 03:19:27 EST 2006


yuppie wrote:
> Hi!
> 
> 
> The skin scripts for complex forms in CMF like folder_contents are
> currently big monolithic blocks of code. All the values needed in the
> template are computed in a predefined order that makes sure expensive
> tasks like querying the catalog or listing folder contents are performed
> only once (per request).
> 
> Trying to convert this into a browser view and to split the code in
> several methods I stumbled other the following issue:
> 
> If globally needed values are returned by their own method they have to
> be computed again and again, although during the short live of a view
> class they can be considered static.
> 
> One option would be to pre-compute those values in the __call__ method
> and to store them in the view object. An other option is to cache the
> results.
> 
> I ended up using this method as decorator for most methods:
> 
> def memoize(func):
>     memo = {}
>     def memoized_func(*args):
>         if args not in memo:
>             memo[args] = func(*args)
>         return memo[args]
>     return memoized_func
> 

This would usable only if all args are hashable. Example:

>>> {}[{1:1},]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: dict objects are unhashable
>>> {}[[1],]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: list objects are unhashable


> 
> Are there better ways to resolve this?
> 
> Will those memo dicts be removed together with the view object or does
> this create a potential memory leak? (I'm not very familiar with
> decorators.)
> 
> 
> Cheers,
> 
>     Yuppie
> 
> 
> _______________________________________________
> Zope-CMF maillist  -  Zope-CMF at lists.zope.org
> http://mail.zope.org/mailman/listinfo/zope-cmf
> 
> See http://collector.zope.org/CMF for bug reports and feature requests
> 



More information about the Zope-CMF mailing list