def get_hash(self, x):
# TODO: cached for strings and paths.
# Need to generalize the reading of:
# 1) file paths
# 2) small strings
# 3) python_objects
if isinstance(x, (dict, bytes)) or not os.path.isfile(x): # use function cache.
return self._get_hash_from_hashable(self._transform_to_hashable(x))
# For files.
hash_obj = self.hash_function()
iter_of_bytes = open(x, 'rb')
try:
data = iter_of_bytes.read(io.DEFAULT_BUFFER_SIZE)
while data:
hash_obj.update(data)
data = iter_of_bytes.read(io.DEFAULT_BUFFER_SIZE)
finally:
iter_of_bytes.close()
return hash_obj.digest()
评论列表
文章目录