Reputation: 7574
I want to generate a hash based on the parameters of the function (for some quick automated caching). The hash should be consistent between different sessions.
Let's assume I have the following functions:
def generate_hash(dictionary):
return hashlib.sha224(str(dictionary.items())).hexdigest()
def foo(a,b,c):
return generate_hash(locals())
Which is perfectly fine as long as the arguments str
representation is consistent across sessions. The problem is that often I pass function as arguments and looks like they are not.
For example, the following call will return a different result across sessions.
foo(1,2,np.sum)
Any workaround?
Upvotes: 2
Views: 409
Reputation: 7574
Seems like I figured something out myself:
16 def generate_hash(dictionary):
17 args = []
18 for key,item in dictionary.iteritems():
19 if isinstance(item,functools.partial):
20 args.append((key,item.func.__module__,item.func.__name__,
21 generate_hash(item.keywords)))
22 elif inspect.isfunction(item):
23 args.append((key,item.__module__,item.__name__))
24 else:
25 args.append((key,item))
26 return hashlib.sha224(str(args)).hexdigest()
which also works consistenly with partial functions:
foo('a','1',np.average) -> 1631c5fd0050fd01cb7a7ee9666d366b35c1415cb4181c7220ead043
foo('a',1,functools.partial(np.average,axis=0)) -> 692227d3b52b0cdcd4ed2204650cb207c1ab6f274a09977c711d35d5
foo('a',1,functools.partial(np.average,axis=1)) -> ba1e0b01f2e12ef1c9ca2e3bf5235aaadcbe4ab29d9c977e1ee6e799
Upvotes: 1