|
11 | 11 | #import dill
|
12 | 12 | import hashlib
|
13 | 13 | import struct
|
| 14 | +from collections import Sequence |
| 15 | +from pickle import PickleError |
14 | 16 |
|
15 | 17 | import numpy as np
|
16 | 18 |
|
@@ -106,10 +108,35 @@ def hash_obj(obj, hash_to='int', full_hash=True):
|
106 | 108 | if not isinstance(obj, basestring):
|
107 | 109 | try:
|
108 | 110 | 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 |
113 | 140 | obj = pkl
|
114 | 141 |
|
115 | 142 | if full_hash:
|
|
0 commit comments