|
8 | 8 | class Node(object):
|
9 | 9 | """Node base"""
|
10 | 10 | def __init__(self, parents, name, *args, **kwargs):
|
11 |
| - parent_hashes = [parent._hash for parent in parents] |
| 11 | + parent_hashes = [hash(parent) for parent in parents] |
12 | 12 | assert len(parent_hashes) == len(set(parent_hashes)), 'Same node cannot be included as parent multiple times'
|
13 | 13 | self._parents = parents
|
| 14 | + self._hash = None |
14 | 15 | self._name = name
|
15 | 16 | self._args = args
|
16 | 17 | self._kwargs = kwargs
|
17 |
| - self._update_hash() |
18 | 18 |
|
19 | 19 | def __repr__(self):
|
20 | 20 | formatted_props = ['{}'.format(arg) for arg in self._args]
|
21 | 21 | formatted_props += ['{}={!r}'.format(key, self._kwargs[key]) for key in sorted(self._kwargs)]
|
22 | 22 | return '{}({})'.format(self._name, ','.join(formatted_props))
|
23 | 23 |
|
24 | 24 | def __hash__(self):
|
25 |
| - return int(self._hash, base=16) |
| 25 | + if self._hash is None: |
| 26 | + self._update_hash() |
| 27 | + return self._hash |
26 | 28 |
|
27 | 29 | def __eq__(self, other):
|
28 |
| - return self._hash == other._hash |
| 30 | + return hash(self) == hash(other) |
29 | 31 |
|
30 | 32 | def _update_hash(self):
|
31 | 33 | props = {'args': self._args, 'kwargs': self._kwargs}
|
32 |
| - my_hash = hashlib.md5(json.dumps(props, sort_keys=True).encode('utf-8')).hexdigest() |
33 |
| - parent_hashes = [parent._hash for parent in self._parents] |
| 34 | + props_str = json.dumps(props, sort_keys=True).encode('utf-8') |
| 35 | + my_hash = hashlib.md5(props_str).hexdigest() |
| 36 | + parent_hashes = [str(hash(parent)) for parent in self._parents] |
34 | 37 | hashes = parent_hashes + [my_hash]
|
35 |
| - self._hash = hashlib.md5(','.join(hashes).encode('utf-8')).hexdigest() |
| 38 | + hashes_str = ','.join(hashes).encode('utf-8') |
| 39 | + hash_str = hashlib.md5(hashes_str).hexdigest() |
| 40 | + self._hash = int(hash_str, base=16) |
36 | 41 |
|
37 | 42 |
|
38 | 43 | class InputNode(Node):
|
|
0 commit comments