473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memoization/Caching of Instance Methods

In the code below, the class DifferentCache utilizes three different
memoization (caching) strategies. Neither the function Memoize1 or
the class Memoize2 will be adequate for all three of these cases (I
intend these to be used as, for example,
getInstanceValueFunction = Memoize1(getInstanceValueFunction)
within the DifferentCache class definition).
Memoize1 will have problems with getMemberValueFunction b/c it
will try to generate a cache key for (self, 'arg1', 'arg2') whereas
the actual values only depend on ('val1', 'val2') (though this may be
more of a nuisance then an error).

Memoize2 will have problems with getInstanceValueFunction b/c
instantiating Memoize2 will cause 'self' to refer to the Memoize2
object and not to the DifferentCache object when computing the desired
function (and worse yet, since the self that does reference
DifferentCache is bound to the DifferentCache.getInstanceValueFunction
it is not even passed in as an argument when Memoize2.__call__ is
executed .... apologies if my terminology is off). Actually, would
this problem apply to any use of Memoize2 to any instance method?
Also, for any of these memoizations, there is only 1 self so we really
don't need it as a cache key.

Both Memoize methods will have problems, in addition, b/c the hashing
for an object is based on __str__ which should also be Memoized --
that is, there is a circular dependency of __hash__ on __str__ and of
__str__ on __hash__. Perhaps a generator that computed the value the first
time and then subsequently return that value from a stored variable?

Obviously, you could deal with these problems by simply keeping each
individual cacheing strategy for the different methods. However, can
anyone see how to unify these into a common cacheing mechanism? Or,
can it be handled by some careful rewriting of how the functions
(methods) are called?
#
# from ?? on comp.lang.python
#
def Memoize1(func):
cache = {}
def _internal(*args):
if cache.has_key(args):
return cache[args]
else:
ans = cache[args] = func(*args)
return ans
return _internal

#
# from Peter norvig on comp.lang.python
#
class Memoize2:
def __init__(self, fn):
self.cache={}
self.fn=fn
def __call__(self,*args):
if self.cache.has_key(args):
return self.cache[args]
else:
object = self.cache[args] = self.fn(*args)
return object
#
# an example class
#
class DifferentCache:
def __init__(self, value):
self.value = value
self.complexValueCache = {}

def getInstanceValueFunction(self):
try:
value = self.instanceValueCache
except KeyError:
value = self.instanceValueCache = someFunction(self)
return value

def getMemberValueFunction(self, other1, other2):
try:
value = self.complexValueCache[other]
except KeyError:
value = self.complexValueCache[other] = \
someOtherFunction(self, other1, other2))
return value

def __str__(self):
try:
strValue = self.stringCache
except AttributeError:
strValue = self.stringCache = str(self.value)
return strValue

def nonCachedFunction(self):
return yetAnotherFunction(self)

def __hash__(self):
return str.__hash__(str(self))
#
# Desired rewrite or so
#
class DifferentCache:
def __init__(self, value):
self.value = value

@Memoized
def getInstanceValueFunction(self):
return someFunction(self)

@Memoized
def getMemberValueFunction(self, other1, other2):
return someOtherFunction(self, other1, other2)

@Memoized
def __str__(self):
return str(self.value)

def nonCachedFunction(self):
return yetAnotherFunction(self)

def __hash__(self):
return str.__hash__(str(self))

Note, the various functions within the instance methods are meant to
represent some arbitrary code executed on and computing values from the
various arguments.

Jul 18 '05 #1
0 1358

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: J. Baute | last post by:
I'm caching data in the Application object to speed up certain pages on a website The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data...
1
by: Bob Rock | last post by:
Hello, I'd like to have some suggestions/pointers to how I could implement caching of generic data used by a set of web methods. I don't need to cache the response of web methods but of generic...
15
by: olle | last post by:
Hi folks. I learning asp.net and compare it with traditional asp and Access-developing. The issue is this one: 1/I have this Ms Acceess adp-project application that works fine on my Ms Sql...
1
by: Gavin Pollock | last post by:
Is anyone using Caching (HttpRuntime.Cache) in Whidbey? Not sure if there's another newsgroup for this though since it's still beta.... I'm having issues running a system built on 1.1 in a 2.0...
2
by: George1776 | last post by:
All, I've recently upgraded our production ASP.NET/C# application from framework 1.1 to 2.0. Since then I've been plagued by out-of-memory errors and problems with the cache object (which may...
4
by: Henrik Dahl | last post by:
Hello! In my application I have a need for using a regular expression now and then. Often the same regular expression must be used multiple times. For performance reasons I use the...
7
by: mark4asp | last post by:
How can I prevent Caching of JavaScript and CSS files ONLY when I deploy a new application? I only want to force a refresh the first time the client uses the new build. For instance, I'm told I...
5
by: trss | last post by:
Has anyone experienced automatic memoization by any C++ compiler before? The program coded as a solution for the problem based on the famous 3n +1 problem, both of which are given below, is...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.