Skip to content

Commit

Permalink
Coerce Parameter kwargs to tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
loganbvh committed Sep 21, 2023
1 parent 0967684 commit 930f73b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tdgl/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,24 @@ def __init__(self, func: Callable, time_dependent: bool = False, **kwargs):
)

def _hash_args(self, x, y, z, t) -> str:
def _coerce_to_tuple(a):
try:
return tuple(_coerce_to_tuple(i) for i in a)
except TypeError:
return a

def _to_tuple(items):
results = []
for key, value in items:
if isinstance(value, dict):
value = _to_tuple(value.items())
elif isinstance(value, (list, np.ndarray)):
value = _coerce_to_tuple(value)
results.append((key, value))
return tuple(results)

return (
hex(hash(tuple(self.kwargs.items())))
hex(hash(_to_tuple(self.kwargs.items())))
+ hashlib.sha1(np.ascontiguousarray(x)).hexdigest()
+ hashlib.sha1(np.ascontiguousarray(y)).hexdigest()
+ hashlib.sha1(np.ascontiguousarray(z)).hexdigest()
Expand Down

0 comments on commit 930f73b

Please sign in to comment.