Skip to content

Commit

Permalink
[HOTFIX] Fix hash key calculation when using non-hashable function ar…
Browse files Browse the repository at this point in the history
…guments.
  • Loading branch information
dsavoiu committed Aug 27, 2019
1 parent 5018585 commit eb3153d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 10 additions & 4 deletions PostProcessing/python/Palisade/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ class HashableMap(Mapping):
"""

def __init__(self, *args, **kwargs):

self._d = dict(*args, **kwargs)

# validate values
for key, value in six.iteritems(self._d):
# make lists hashable by casting to tuple
if isinstance(value, list):
value = tuple(value)

# check hashability
try:
hash(value)
except TypeError:
# attempt to wrap non-hashable types
if isinstance(value, list) or isinstance(value, tuple):
print("Wrap list/tuple: ", value)
value = tuple(enumerate(value))
elif isinstance(value, dict):
print("Wrap dict: ", value)
value = tuple(six.iteritems(value))
else:
raise

self._d[key] = HashableMap(value)

self._hash = None # computed on request
Expand Down
16 changes: 16 additions & 0 deletions PostProcessing/test/Palisade/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ def triple_memoized(tobject):
# remove function to avoid side effects
InputROOT.functions.pop('triple_memoized', None)

def test_get_expr_user_defined_function_memoized_list_arguments(self):

# add a custom function: scale hist by a factor 3
@InputROOT.add_function(memoize=True)
def how_many(tobjects):
return len(tobjects)

# evaluate expr and compute reference reult
_result_expr = self._ic.get_expr('how_many(["test:h1", "test:h1", "test:h1"])')

# bin-by-bin comparison
self.assertEqual(_result_expr, 3)

# remove function to avoid side effects
InputROOT.functions.pop('how_many', None)

def test_get_expr_local_variables(self):

with self.subTest(test_label="call_local_variable"):
Expand Down

0 comments on commit eb3153d

Please sign in to comment.