From 7a1b37110278d7e2d5b97bba09b20cd8d1ebd846 Mon Sep 17 00:00:00 2001 From: Ming Du Date: Wed, 22 May 2024 10:25:42 -0500 Subject: [PATCH] Beautify dumped config json --- generic_trainer/configs.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/generic_trainer/configs.py b/generic_trainer/configs.py index 16fc8ef..84105cc 100644 --- a/generic_trainer/configs.py +++ b/generic_trainer/configs.py @@ -38,7 +38,9 @@ def get_serializable_dict(self): v = self.__dict__[key] if not self.__class__.is_jsonable(v): if isinstance(v, (tuple, list)): - v = '_'.join([str(x) for x in v]) + v = [str(x) for x in v] + elif isinstance(v, OptionContainer): + v = v.get_serializable_dict() else: v = str(v) d[key] = v @@ -48,7 +50,7 @@ def dump_to_json(self, filename): try: f = open(filename, 'w') d = self.get_serializable_dict() - json.dump(d, f) + json.dump(d, f, indent=4, separators=(',', ': ')) f.close() except: print('Failed to dump json.') @@ -63,6 +65,23 @@ def load_from_json(self, filename): self.__dict__[key] = d[key] f.close() + def string_to_object(self, key, value): + """ + Create an object based on the string value of a config key. + :param key: str. + :param value: str. + :return: object. + """ + pass + + def object_to_string(self, key): + """ + Convert an object in a config key to string. + :param key: str. + :return: str. + """ + pass + # ============================= # Model parameter classes