Skip to content

Commit

Permalink
memoize: also support numpy arrays and objects with __hash__() functions
Browse files Browse the repository at this point in the history
- Bump to 0.15.1
  • Loading branch information
smathot committed Aug 22, 2022
1 parent b75faa3 commit 008784a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamatrix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
from datamatrix._datamatrix._nifticolumn import NiftiColumn
from datamatrix._datamatrix._datamatrix import DataMatrix

__version__ = '0.15.0'
__version__ = '0.15.1'
NAN = float('nan')
INF = float('inf')
8 changes: 8 additions & 0 deletions datamatrix/_functional/_memoize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
from collections import OrderedDict
import hashlib
import pickle
try:
import numpy as np
except ImportError:
np = None


ONE_GIGABYTE = 1024**3
Expand Down Expand Up @@ -325,6 +329,8 @@ def _serialize_obj(self, obj):

import json_tricks

if hasattr(obj, '__hash__') and callable(obj.__hash__):
return obj.__hash__()
if callable(obj):
return obj.__name__ if hasattr(obj, '__name__') else '__nameless__'
if isinstance(obj, dict):
Expand All @@ -336,6 +342,8 @@ def _serialize_obj(self, obj):
return self._serialize_args(obj)
if isinstance(obj, DataMatrix):
return cnv.to_json(obj)
if np is not None and isinstance(obj, np.ndarray):
return str(obj.data.tobytes())
return json_tricks.dumps(obj)

def _serialize_args(self, args):
Expand Down

0 comments on commit 008784a

Please sign in to comment.