diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index 062cb96..10eac09 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -152,10 +152,34 @@ class _PickleCore(_BaseCore): return key, self._get_cache().get(key, None) def get_entry(self, args, kwds): - key = args + tuple(sorted(kwds.items())) - # print('key type={}, key={}'.format(type(key), key)) + key = tuple(self.hash_args(key) for key in args + tuple(sorted(kwds.items()))) return self.get_entry_by_key(key) + def hash_args(self, value): + try: + import pandas + if isinstance(value, pandas.DataFrame): + return(pandas.util.hash_pandas_object(value)) + except ImportError: + pass + if hasattr(value, "to_bytes"): # For numpy + try: + return hash(value.to_bytes()) + except TypeError: + pass + elif hasattr(value, "__iter__"): # For iterators + hash_array = [] + for elem in value: + hash_array.append(value) + return tuple(hash_array) + elif hasattr(value, "items"): # For dict + hash_array = [] + for key, elem in value.items: + hash_array.append(key) + hash_array.append(elem) + return tuple(hash_array) + return hash(value) + def set_entry(self, key, func_res): with self.lock: cache = self._get_cache()