-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle in a better way decoration of class methods. #7
Comments
I tried your method to cache some computation for a class but I does not seem to work for methods of an object:
which prints
This seems to be fixable by adding something in the prefix. |
Hello @ngast, as you have surely noticed this is a known issue that we are trying to understand how to solve best. The issue is that one solution may take the form of hashing the entire class (the And yet another option is that users may be interested in altogether ignore the self method. Could you share your opinion on this issue? We have been struggling for a while now about how to proceed to fix this. |
I am not sure if this would be the best option but the way I intended to do (before looking for an already existing solution) would have been to create a method for my class like "hash_of_instance(self)" that I would correspond to a hash of the interesting parameters. Then, I would concatenate this hash to the filename of the cache. |
Hi, sorry for the long wait, we finally figured out how to properly handle methods. class A:
"""Test that we can hash methods if self implements Hashable"""
def __init__(self, x):
self.x = x
@Cache(
cache_path="{cache_dir}/{a.name}_{self.x}.pkl",
cache_dir="./test_cache",
backup=False,
)
def cached_function(self, a):
sleep(2)
return [1, 2, 3] Or you can implement your custom hash: from dict_hash import Hashable
class B(Hashable):
"""Test that we can hash methods if self implements Hashable"""
def __init__(self, x):
self.x = x
@Cache(
cache_path="{cache_dir}/{a}_{_hash}.pkl",
cache_dir="./test_cache",
backup=False,
)
def cached_function(self, a):
sleep(2)
return [1, 2, 3]
def consistent_hash(self) -> str:
return str(self.x) I hope this will solve this problem! |
No description provided.
The text was updated successfully, but these errors were encountered: