From 12dfde423bcf0a2027c45328d16a03dd25cfe617 Mon Sep 17 00:00:00 2001 From: hanhxiao Date: Fri, 19 Jul 2019 16:02:21 +0800 Subject: [PATCH] fix(base): move name setting to trainable base --- gnes/base/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gnes/base/__init__.py b/gnes/base/__init__.py index bb7f884c..1dee9f81 100644 --- a/gnes/base/__init__.py +++ b/gnes/base/__init__.py @@ -83,16 +83,6 @@ def __call__(cls, *args, **kwargs): v = kwargs[k] setattr(obj, k, v) - if not getattr(obj, 'name', None): - _id = str(uuid.uuid4()).split('-')[0] - _name = '%s-%s' % (obj.__class__.__name__, _id) - obj.logger.warning( - 'this object is not named ("- gnes_config: - name" is not found in YAML config), ' - 'i will call it as "%s". ' - 'naming the object is important especially when you need to ' - 'serialize/deserialize/store/load the object.' % _name) - setattr(obj, 'name', _name) - getattr(obj, '_post_init_wrapper', lambda *x: None)() return obj @@ -170,6 +160,16 @@ def __init__(self, *args, **kwargs): self._post_init_vars = set() def _post_init_wrapper(self): + if not getattr(self, 'name', None): + _id = str(uuid.uuid4()).split('-')[0] + _name = '%s-%s' % (self.__class__.__name__, _id) + self.logger.warning( + 'this object is not named ("- gnes_config: - name" is not found in YAML config), ' + 'i will call it as "%s". ' + 'naming the object is important especially when you need to ' + 'serialize/deserialize/store/load the object.' % _name) + setattr(self, 'name', _name) + _before = set(list(self.__dict__.keys())) self.post_init() self._post_init_vars = {k for k in self.__dict__ if k not in _before}