diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index f7cac16..fd40a27 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -152,7 +152,10 @@ class _PickleCore(_BaseCore): return key, self._get_cache().get(key, None) def get_entry(self, args, kwds): - key = tuple(self._hash_args(key) for key in args + tuple(sorted(kwds.items()))) + 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): @@ -160,7 +163,7 @@ class _PickleCore(_BaseCore): import pandas if isinstance(value, pandas.DataFrame): return pandas.util.hash_pandas_object(value).sum() - except ImportError: + except ImportError: # pragma: no cover pass if hasattr(value, "tobytes"): # For numpy return hash(value.tobytes()) @@ -172,7 +175,7 @@ class _PickleCore(_BaseCore): return tuple(hash_array) if hasattr(value, "items"): # For dict hash_array = [] - for key, elem in value.items: + for key, elem in value.items(): hash_array.append(key) hash_array.append(self._hash_args(elem)) return tuple(hash_array) diff --git a/tests/test_pickle_list_dict.py b/tests/test_pickle_list_dict.py new file mode 100644 index 0000000..725e5b4 --- /dev/null +++ b/tests/test_pickle_list_dict.py @@ -0,0 +1,62 @@ +"""Testing pickling of lists and dicts for the Cachier python package.""" + +# This file is part of Cachier. +# https://github.com/shaypal5/cachier + +# Licensed under the MIT license: +# http://www.opensource.org/licenses/MIT-license +# Copyright (c) 2016, Shay Palachy + +from time import sleep, time + +from cachier import cachier + + +@cachier() +def _list_takes_2_seconds(a): + """ Numpy cache """ + sleep(2) + return a * 2 + + +def test_list(): + a = [1, 2, 3] + _list_takes_2_seconds.clear_cache() + _list_takes_2_seconds(a) + start = time() + _list_takes_2_seconds(a) + end = time() + assert end - start < 1 + + a = [1, 2, 4] + start = time() + _list_takes_2_seconds(a) + end = time() + assert end - start > 2.0 + + _list_takes_2_seconds.clear_cache() + + +@cachier() +def _dict_takes_2_seconds(a): + """ Numpy cache """ + sleep(2) + return {k: 2 * v for k, v in a.items()} + + +def test_dict(): + a = {'a': 1, 'b': 2} + _dict_takes_2_seconds.clear_cache() + _dict_takes_2_seconds(a) + start = time() + _dict_takes_2_seconds(a) + end = time() + assert end - start < 1 + + a = {'a': 1, 'b': 3} + start = time() + _dict_takes_2_seconds(a) + end = time() + assert end - start > 2.0 + + _dict_takes_2_seconds.clear_cache()