Skip to content

Commit

Permalink
Move all type handlings to object_to_string in OptionContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
mdw771 committed May 22, 2024
1 parent 7a1b371 commit 31147b2
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions generic_trainer/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ def get_serializable_dict(self):
d = {}
for key in self.__dict__.keys():
v = self.__dict__[key]
if not self.__class__.is_jsonable(v):
if isinstance(v, (tuple, list)):
v = [str(x) for x in v]
elif isinstance(v, OptionContainer):
v = v.get_serializable_dict()
else:
v = str(v)
v = self.object_to_string(key, v)
d[key] = v
return d

Expand All @@ -62,7 +56,7 @@ def load_from_json(self, filename):
f = open(filename, 'r')
d = json.load(f)
for key in d.keys():
self.__dict__[key] = d[key]
self.__dict__[key] = self.string_to_object(key, d[key])
f.close()

def string_to_object(self, key, value):
Expand All @@ -72,15 +66,22 @@ def string_to_object(self, key, value):
:param value: str.
:return: object.
"""
pass
return value

def object_to_string(self, key):
def object_to_string(self, key, value):
"""
Convert an object in a config key to string.
:param key: str.
:param value: object.
:return: str.
"""
pass
if isinstance(value, OptionContainer):
value = value.get_serializable_dict()
elif isinstance(value, (tuple, list)):
value = [self.object_to_string(key, x) for x in value]
else:
value = str(value)
return value


# =============================
Expand Down

0 comments on commit 31147b2

Please sign in to comment.