diff --git a/cadetrdm/configuration_options.py b/cadetrdm/configuration_options.py index 115314f3eb2d6fc7861f4d3ca921bac82d9bdcbc..9c25b5805d1464645e417390be47245bc1cab9e9 100644 --- a/cadetrdm/configuration_options.py +++ b/cadetrdm/configuration_options.py @@ -1,3 +1,4 @@ +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__()) diff --git a/tests/test_configuration_options.py b/tests/test_configuration_options.py index 2ce44b3d062f8526455243e057aa2284e8109e0d..ae0e87a3026e1b612fd8938b206b81ae14da32af 100644 --- a/tests/test_configuration_options.py +++ b/tests/test_configuration_options.py @@ -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():