8000 Clean up hashing · Powercoder64/ffmpeg-python@f1e6212 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1e6212

Browse files
committed
Clean up hashing
1 parent e3a846b commit f1e6212

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ffmpeg/nodes.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,36 @@
88
class Node(object):
99
"""Node base"""
1010
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]
1212
assert len(parent_hashes) == len(set(parent_hashes)), 'Same node cannot be included as parent multiple times'
1313
self._parents = parents
14+
self._hash = None
1415
self._name = name
1516
self._args = args
1617
self._kwargs = kwargs
17-
self._update_hash()
1818

1919
def __repr__(self):
2020
formatted_props = ['{}'.format(arg) for arg in self._args]
2121
formatted_props += ['{}={!r}'.format(key, self._kwargs[key]) for key in sorted(self._kwargs)]
2222
return '{}({})'.format(self._name, ','.join(formatted_props))
2323

2424
def __hash__(self):
25-
return int(self._hash, base=16)
25+
if self._hash is None:
26+
self._update_hash()
27+
return self._hash
2628

2729
def __eq__(self, other):
28-
return self._hash == other._hash
30+
return hash(self) == hash(other)
2931

3032
def _update_hash(self):
3133
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]
3437
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)
3641

3742

3843
class InputNode(Node):

0 commit comments

Comments
 (0)
0