Skip to content

Commit

Permalink
mostly fixes for linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jkitchin committed Jan 28, 2025
1 parent 2629559 commit ac52668
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions pycse/hashcache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""hashcache - a class decorator for persistent, file/hash-based cache
"""hashcache - a class decorator for persistent, file/hash-based cache.
I found some features of joblib were unsuitable for how I want to use a cache.
Expand Down Expand Up @@ -91,16 +91,26 @@


def hashcache(*args, **kwargs):
"""Raises an exception if the old hashcache decorator is used."""
"""Raise an exception if the old hashcache decorator is used."""
raise Exception(
"The hashcache function decorator is deprecated." " Please use the class decorator instead."
"""The hashcache function decorator is deprecated.
Please use the class decorator instead.
For example:
from pycse.hashcache import HashCache
@HashCache
def f(x):
return x
"""
)


class HashCache:
"""Class decorator to cache using hashes and pickle (via joblib).
Data is stored in directories named by the hash.
Data is stored in directories named by the hash.
"""

# cache is the name of the directory to store results in
Expand All @@ -109,10 +119,11 @@ class HashCache:
verbose = False

def __init__(self, function):
"""Decorate the function."""
self.function = function

def get_standardized_args(self, args, kwargs):
"""Returns a standardized dictionary of kwargs for func(args, kwargs)
"""Return a standardized dictionary of kwargs for func(args, kwargs).
This dictionary includes default values, even if they were not called.
Expand All @@ -125,9 +136,9 @@ def get_standardized_args(self, args, kwargs):
def get_hash(self, args, kwargs):
"""Get a hash for running FUNC(ARGS, KWARGS).
This is the most critical feature of hashcache as it provides a key to store
and look up results later. You should think carefully before changing this
function, it breaks past caches.
This is the most critical feature of hashcache as it provides a key to
store and look up results later. You should think carefully before
changing this function, it breaks past caches.
FUNC should be as pure as reasonable. This hash is insensitive to global
variables.
Expand All @@ -144,7 +155,8 @@ def get_hash(self, args, kwargs):
[
self.function.__code__.co_name, # This is the function name
self.function.__code__.co_code, # this is the function bytecode
self.get_standardized_args(args, kwargs), # The args used, including defaults
# The args used, including defaults
self.get_standardized_args(args, kwargs),
],
hash_name="sha1",
)
Expand Down Expand Up @@ -190,8 +202,7 @@ def dump_data(self, hsh, data):
return files

def __call__(self, *args, **kwargs):
"""This is the decorator code that runs around self.function."""

"""Code to run around self.function."""
hsh = self.get_hash(args, kwargs)

# Try getting the data first
Expand Down Expand Up @@ -241,6 +252,7 @@ def __call__(self, *args, **kwargs):
@staticmethod
def dump(**kwargs):
"""Dump KWARGS to the cache.
Returns a hash string for future lookup.
cache is a special kwarg that is not saved
Expand Down Expand Up @@ -289,6 +301,7 @@ def load(hsh, cache="cache"):

class SqlCache(HashCache):
"""Class decorator to cache using orjson and sqlite.
Data is stored in a sqlite database as json.
"""
Expand All @@ -300,13 +313,15 @@ class SqlCache(HashCache):
default = None

def __init__(self, function):
"""Initialize the class."""
self.function = function

self.con = sqlite3.connect(self.cache)
self.con.execute("CREATE TABLE if not exists cache(hash TEXT unique, value TEXT)")

def dump_data(self, hsh, data):
"""Dump DATA into HSH.
DATA must be serializable to json.
"""
Expand Down Expand Up @@ -334,6 +349,7 @@ def load_data(self, hsh):
@staticmethod
def search(query, *args):
"""Run a sql QUERY with args.
args are substituted in ? placeholders in the query.
This is just a light wrapper on con.execute.
Expand All @@ -346,6 +362,7 @@ def search(query, *args):
@staticmethod
def dump(**kwargs):
"""Dump KWARGS to the cache.
Returns a hash string for future lookup.
"""
t0 = time.time()
Expand Down Expand Up @@ -377,7 +394,6 @@ def dump(**kwargs):
@staticmethod
def load(hsh):
"""Load data from HSH."""

hc = SqlCache(lambda x: x)
with hc.con:
cur = hc.con.execute("SELECT value FROM cache WHERE hash = ?", (hsh,))
Expand All @@ -394,6 +410,7 @@ class JsonCache(HashCache):
default = None

def __init__(self, function):
"""Initialize the class."""
self.function = function

if not os.path.exists(self.cache / Path("Filestore.json")):
Expand All @@ -410,6 +427,7 @@ def dump_data(self, hsh, data):
f.write(orjson.dumps(data, default=self.default, option=orjson.OPT_SERIALIZE_NUMPY))

def load_data(self, hsh):
"""Load data from hsh."""
hshpath = self.get_hashpath(hsh).with_suffix(".json")
if os.path.exists(hshpath):
with open(hshpath, "rb") as f:
Expand All @@ -425,6 +443,7 @@ def load_data(self, hsh):
@staticmethod
def dump(**kwargs):
"""Dump KWARGS to the cache.
Returns a hash string for future lookup.
"""
t0 = time.time()
Expand Down Expand Up @@ -457,7 +476,6 @@ def dump(**kwargs):
@staticmethod
def load(hsh):
"""Load data from HSH."""

hc = JsonCache(lambda x: x)
hshpath = hc.get_hashpath(hsh).with_suffix(".json")
if os.path.exists(hshpath):
Expand Down

0 comments on commit ac52668

Please sign in to comment.