From 6ecf666b293db8452a5e16ef329502ad7e32222e Mon Sep 17 00:00:00 2001 From: fx_kirin Date: Wed, 8 Jan 2020 13:03:34 +0900 Subject: [PATCH] add test --- cachier/pickle_core.py | 9 ++--- tests/test_numpy_pandas.py | 83 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 tests/test_numpy_pandas.py diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index 10eac09..046f810 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -159,14 +159,11 @@ class _PickleCore(_BaseCore): try: import pandas if isinstance(value, pandas.DataFrame): - return(pandas.util.hash_pandas_object(value)) + return(pandas.util.hash_pandas_object(value).sum()) except ImportError: pass - if hasattr(value, "to_bytes"): # For numpy - try: - return hash(value.to_bytes()) - except TypeError: - pass + if hasattr(value, "tobytes"): # For numpy + return hash(value.tobytes()) elif hasattr(value, "__iter__"): # For iterators hash_array = [] for elem in value: diff --git a/tests/test_numpy_pandas.py b/tests/test_numpy_pandas.py new file mode 100644 index 0000000..434821f --- /dev/null +++ b/tests/test_numpy_pandas.py @@ -0,0 +1,83 @@ +"""Test 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 os.path import ( +# realpath, +# dirname +# ) +import os +from time import time, sleep +from datetime import timedelta +from random import random +import threading + +try: + import queue +except ImportError: # python 2 + import Queue as queue + +from cachier import cachier +from cachier.pickle_core import DEF_CACHIER_DIR + +import numpy as np +import pandas as pd + +# Pickle core tests + + +@cachier() +def _numpy_sum_takes_2_seconds(a): + """ Numpy cache """ + sleep(2) + return a.sum() + + +@cachier() +def _pandas_sum_takes_2_seconds(df): + """ Numpy cache """ + sleep(2) + return df.sum() + + +def test_numpy_narray(): + """Basic numpy core functionality.""" + a = np.zeros(1000) + _numpy_sum_takes_2_seconds.clear_cache() + _numpy_sum_takes_2_seconds(a) + start = time() + _numpy_sum_takes_2_seconds(a) + end = time() + assert end - start < 1 + + a[0] = 3 + start = time() + _numpy_sum_takes_2_seconds(a) + end = time() + assert end - start > 2.0 + + _numpy_sum_takes_2_seconds.clear_cache() + + +def test_pandas_dataframe(): + """Basic Pickle core functionality.""" + a = np.zeros(1000) + df = pd.DataFrame(a) + _numpy_sum_takes_2_seconds.clear_cache() + _numpy_sum_takes_2_seconds(df) + start = time() + _numpy_sum_takes_2_seconds(df) + end = time() + assert end - start < 1 + _numpy_sum_takes_2_seconds.clear_cache() + + df.iloc[0, 0] = 3 + start = time() + _numpy_sum_takes_2_seconds(a) + end = time() + assert end - start > 2.0