From 9531894eee56b1e151869b86abef8ec7ceb95c3d Mon Sep 17 00:00:00 2001 From: Shay Palachy Date: Thu, 20 Feb 2020 13:14:34 +0200 Subject: [PATCH] Revert commit: 94778a0b66a8c2eeb9de3c2a5cd50fe9883b6d1f --- .gitignore | 3 --- .travis.yml | 2 -- README.rst | 2 +- cachier/pickle_core.py | 30 ++-------------------------- cachier_text_cache_temp.pkl | 1 + pytest.ini | 1 - setup.py | 2 +- tests/test_pickle_core.py | 40 ------------------------------------- 8 files changed, 5 insertions(+), 76 deletions(-) create mode 100644 cachier_text_cache_temp.pkl diff --git a/.gitignore b/.gitignore index 0d8c995..8d11232 100644 --- a/.gitignore +++ b/.gitignore @@ -101,6 +101,3 @@ cachier_test_mongo_cred.yml # Pipfile doesn't work for me yet Pipfile Pipfile.lock - -# pickle files -*.pkl diff --git a/.travis.yml b/.travis.yml index 5c41608..aa2ef12 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ python: - 3.5 - 3.6 - 3.7 - - 3.8 - "3.6-dev" - "3.7-dev" - "3.8-dev" @@ -86,7 +85,6 @@ install: - echo $TRAVIS_OS_NAME - pip install ".[test]" # - if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ] && ["$TRAVIS_OS_NAME" == "linux"]; then pip install coverage pytest-cov .; else pip install ".[test]"; fi -before_script: pytest -m prep script: pytest after_success: - codecov # submit coverage to codecov.io diff --git a/README.rst b/README.rst index 735f910..1a15e84 100644 --- a/README.rst +++ b/README.rst @@ -69,7 +69,7 @@ Future features Use === -Cachier provides a decorator which you can wrap around your functions to give them a persistent cache. The positional and keyword arguments to the wrapped function must be hashable (i.e. Python's immutable built-in objects, not mutable containers), or pickle-able objects. Also, notice that since objects which are instances of user-defined classes are hashable but all compare unequal (their hash value is their id), equal objects across different sessions will not yield identical keys. +Cachier provides a decorator which you can wrap around your functions to give them a persistent cache. The positional and keyword arguments to the wrapped function must be hashable (i.e. Python's immutable built-in objects, not mutable containers). Also, notice that since objects which are instances of user-defined classes are hashable but all compare unequal (their hash value is their id), equal objects across different sessions will not yield identical keys. Setting up a Cache ------------------ diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index a32d1ac..062cb96 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -8,7 +8,6 @@ # Copyright (c) 2016, Shay Palachy import os -from zlib import adler32 import pickle # for local caching from datetime import datetime import threading @@ -153,35 +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 = args + tuple(sorted(kwds.items())) + # print('key type={}, key={}'.format(type(key), key)) 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).sum() - except ImportError: # pragma: no cover - pass - if hasattr(value, "tobytes"): # For numpy - return adler32(value.tobytes()) & 0xffffffff - if hasattr(value, "__iter__"): # For iterators - if isinstance(value, (list, tuple)): - hash_array = [] - for elem in value: - hash_array.append(self._hash_args(elem)) - return tuple(hash_array) - if hasattr(value, "items"): # For dict - hash_array = [] - for key, elem in value.items(): - hash_array.append(key) - hash_array.append(self._hash_args(elem)) - return tuple(hash_array) - return adler32(pickle.dumps(value)) & 0xffffffff - def set_entry(self, key, func_res): with self.lock: cache = self._get_cache() diff --git a/cachier_text_cache_temp.pkl b/cachier_text_cache_temp.pkl new file mode 100644 index 0000000..ee280a7 --- /dev/null +++ b/cachier_text_cache_temp.pkl @@ -0,0 +1 @@ +€G?Ú)Ù[*A¬. \ No newline at end of file diff --git a/pytest.ini b/pytest.ini index 76bef99..09575d5 100644 --- a/pytest.ini +++ b/pytest.ini @@ -11,4 +11,3 @@ addopts = -r a -v -s - -m "not prep" diff --git a/setup.py b/setup.py index 3ffbfb5..c438286 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ except ImportError: import versioneer -TEST_REQUIRES = ['pytest', 'coverage', 'pytest-cov', 'pymongo', 'numpy', 'pandas'] +TEST_REQUIRES = ['pytest', 'coverage', 'pytest-cov', 'pymongo'] README_RST = '' with open('README.rst') as f: diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index 6a4a54a..0fef615 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -16,17 +16,14 @@ from time import ( time, sleep ) -from pickle import load, dump, dumps from datetime import timedelta from random import random -from zlib import adler32 import threading try: import queue except ImportError: # python 2 import Queue as queue -import pytest from cachier import cachier from cachier.pickle_core import DEF_CACHIER_DIR @@ -395,40 +392,3 @@ def test_pickle_core_custom_cache_dir(): assert end - start < 1 _takes_5_seconds_custom_dir.clear_cache() assert _takes_5_seconds_custom_dir.cache_dpath() == EXPANDED_CUSTOM_DIR - - -TEXT_VAL_TO_CHECK = 'foo' -TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl' - - -@cachier() -def text_caching(text): - sleep(1) - print(text) - print(adler32(dumps(text)) & 0xffffffff) - return random() - - -@pytest.mark.prep -def test_prep_text_hashing(): - text_caching.clear_cache() - return_val = text_caching(TEXT_VAL_TO_CHECK) - print(return_val) - with open(TEXT_CACHE_FNAME, 'wb+') as f: - dump(return_val, f) - - -def test_text_hashing(): - with open(TEXT_CACHE_FNAME, 'rb') as f: - first = load(f) - print('\npickled return val found for text cache text:') - print(first) - start_time = time() - print('calling with value:') - print(TEXT_VAL_TO_CHECK) - second = text_caching(TEXT_VAL_TO_CHECK) - print('second value returned:') - print(second) - call_time = time() - start_time - assert call_time < 1 - assert first == second