You can use anycache to do the job for you. It considers all the details:
pickle
module to handle lambda
and all the nice
python features.Assuming you have a function myfunc
which creates the instance:
from anycache import anycache
class Company(object):
def __init__(self, name, value):
self.name = name
self.value = value
@anycache(cachedir='/path/to/your/cache')
def myfunc(name, value)
return Company(name, value)
Anycache calls myfunc
at the first time and pickles the result to a
file in cachedir
using an unique identifier (depending on the function name and its arguments) as filename.
On any consecutive run, the pickled object is loaded.
If the cachedir
is preserved between python runs, the pickled object is taken from the previous python run.
For any further details see the documentation