Hashing a python method to regenerate output when method is modified
- by Seth Johnson
I have a python method that has a deterministic result. It takes a long time to run and generates a large output:
def time_consuming_method():
# lots_of_computing_time to come up with the_result
return the_result
I modify time_consuming_method from time to time, but I would like to avoid having it run again while it's unchanged. [Time_consuming_method only depends on functions that are immutable for the purposes considered here; i.e. it might have functions from Python libraries but not from other pieces of my code that I'd change.] The solution that suggests itself to me is to cache the output and also cache some "hash" of the function. If the hash changes, the function will have been modified, and we have to re-generate the output.
Is this possible or a ridiculous idea?
If this isn't a terrible idea, is the best implementation to write
f = """
def ridiculous_method():
a = #
# lots_of_computing_time
return a
"""
, use the hashlib module to compute a hash for f, and use compile or eval to run it as code?