This commit is contained in:
fx_kirin
2020-01-08 13:03:34 +09:00
parent 3f8750361a
commit 6ecf666b29
2 changed files with 86 additions and 6 deletions
+3 -6
View File
@@ -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:
+83
View File
@@ -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 <shaypal5@gmail.com>
# 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