mirror of
https://github.com/wassname/cachier.git
synced 2026-09-10 11:50:23 +08:00
Revert commit: 94778a0b66
This commit is contained in:
@@ -101,6 +101,3 @@ cachier_test_mongo_cred.yml
|
||||
# Pipfile doesn't work for me yet
|
||||
Pipfile
|
||||
Pipfile.lock
|
||||
|
||||
# pickle files
|
||||
*.pkl
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
------------------
|
||||
|
||||
+2
-28
@@ -8,7 +8,6 @@
|
||||
# Copyright (c) 2016, Shay Palachy <shaypal5@gmail.com>
|
||||
|
||||
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()
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
€G?Ú)Ù[*A¬.
|
||||
@@ -11,4 +11,3 @@ addopts =
|
||||
-r a
|
||||
-v
|
||||
-s
|
||||
-m "not prep"
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user