Skip to content

Commit 49e16e1

Browse files
ShiveshMjllanfranchi
ShiveshM
authored andcommitted
Add hash property to Pipeline and implement a temporary fix to issue #329 (#339)
* Add hash property to Pipeline and implement a temporary fix to issue #329 * Respond to PR comments
1 parent ccd3f35 commit 49e16e1

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: pisa/core/pipeline.py

+5
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@ def state_hash(self):
367367
the state of the corresponding stage."""
368368
return hash_obj([self.source_code_hash] + [s.state_hash for s in self])
369369

370+
@property
371+
def hash(self):
372+
"""High level hash of this object's class"""
373+
return hash_obj((self.source_code_hash, self.state_hash))
374+
370375

371376
def test_Pipeline():
372377
"""Unit tests for Pipeline class"""

Diff for: pisa/utils/hash.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#import dill
1212
import hashlib
1313
import struct
14+
from collections import Sequence
15+
from pickle import PickleError
1416

1517
import numpy as np
1618

@@ -106,10 +108,35 @@ def hash_obj(obj, hash_to='int', full_hash=True):
106108
if not isinstance(obj, basestring):
107109
try:
108110
pkl = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
109-
except:
110-
logging.error('Failed to pickle `obj` "%s" of type "%s"'
111-
%(obj, type(obj)))
112-
raise
111+
except (PickleError, TypeError):
112+
# Iterate through each element if obj is a Sequence
113+
if isinstance(obj, Sequence):
114+
# Store hash of each element
115+
hash_list = []
116+
for ele in obj:
117+
# Try to get hash from property
118+
if hasattr(ele, 'hash'):
119+
hash_list.append(ele.hash)
120+
else:
121+
# Otherwise try to get hash using pickle
122+
try:
123+
a = pickle.dumps(ele, pickle.HIGHEST_PROTOCOL)
124+
hash_list.append(a)
125+
except (PickleError, TypeError):
126+
logging.error('Failed to pickle `ele` "%s" of '
127+
'type "%s"' %(ele, type(ele)))
128+
raise
129+
# Get hash values by pickling the hash list
130+
try:
131+
pkl = pickle.dumps(hash_list, pickle.HIGHEST_PROTOCOL)
132+
except (PickleError, TypeError):
133+
logging.error('Failed to pickle `hash_list` "%s" of type '
134+
'"%s"' %(hash_list, type(hash_list)))
135+
raise
136+
else:
137+
logging.error('Failed to pickle `obj` "%s" of type "%s"'
138+
%(obj, type(obj)))
139+
raise
113140
obj = pkl
114141

115142
if full_hash:

0 commit comments

Comments
 (0)