Skip to content
Snippets Groups Projects
Commit ef86813d authored by r.jaepel's avatar r.jaepel
Browse files

Change Options hash to be unsalted and reproducible

parent 4fb162ad
No related branches found
No related tags found
No related merge requests found
import hashlib
import json
from benedict import BeneDict, benedict_to_dict
......@@ -51,11 +52,26 @@ class Options(BeneDict):
def load_json_str(cls, string, **loader_kwargs):
return cls.loads(string)
def __hash__(self):
return self.dumps().__hash__()
def __hash__(self, excluded_keys=None):
excluded_keys = {"commit_message", "push", "debug", "case"}
remaining_keys = set(self.keys()) - excluded_keys
remaining_dict = {key: self[key] for key in remaining_keys}
dump = json.dumps(
remaining_dict,
cls=NumpyEncoder,
ensure_ascii=False,
sort_keys=True,
indent=None,
separators=(',', ':'),
)
return int(hashlib.md5(dump.encode('utf-8')).hexdigest(), 16)
if __name__ == '__main__':
options = Options()
options.optimizer_options = 10
options.commit_message = "Fuubar"
options_rev = Options.load_json_str(options.dump_json_str())
print(options.dump_json_str())
options_rev.commit_message = "unfoo"
print(options.__hash__(), options_rev.__hash__())
......@@ -9,11 +9,9 @@ def test_options_hash():
opt["nested_dict"] = {"ba": "foo", "bb": "bar"}
initial_hash = hash(opt)
s = opt.dumps()
serialized_hash = hash(s)
opt_recovered = Options.loads(s)
post_serialization_hash = hash(opt_recovered)
assert initial_hash == serialized_hash
assert serialized_hash == post_serialization_hash
assert initial_hash == post_serialization_hash
def test_options_file_io():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment