mirror of
https://github.com/wassname/cachier.git
synced 2026-09-09 11:19:08 +08:00
add test
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user