I am writing a class that has some computation-heavy methods and some parameters that the user will want to iteratively tweak and are independent of the computation.
The actual use is for visualization, but here's a cartoon example:
class MyClass(object):
def __init__(self, x, name, mem=None):
self.x = x
self.name = name
if mem is not None:
self.square = mem.cache(self.square)
def square(self, x):
"""This is the 'computation heavy' method."""
return x ** 2
def report(self):
"""Use the results of the computation and a tweakable parameter."""
print "Here you go, %s" % self.name
return self.square(self.x)
The basic idea is that the user might want to create many instances of this class with the same x
but different name
parameters. I want to allow the user to provide a joblib.Memory
object that will cache the computation part, so they can "report" to lots of different names without recomputing the squared array each time.
(This is a little weird, I know. The reason the user needs a different class instance for each name is that they'll actually be interacting with an interface function that looks like this.
def myfunc(x, name, mem=None):
theclass = MyClass(x, name, mem)
theclass.report()
But let's ignore that for now).
Following the joblib docs I am caching the square
function with the line self.square = mem.cache(self.square)
. The problem is that, because self
will be different for different instances, the array gets recomputed every time even when the argument is the same.
I am guessing that the correct way to handle this is changing the line to
self.square = mem.cache(self.square, ignore=["self"])
However, are there any drawbacks to this approach? Is there a better way to accomplish the caching?