Hashing a python method to regenerate output when method is modified
Posted
by Seth Johnson
on Stack Overflow
See other posts from Stack Overflow
or by Seth Johnson
Published on 2010-04-26T20:41:54Z
Indexed on
2010/04/26
21:03 UTC
Read the original article
Hit count: 275
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?
© Stack Overflow or respective owner