Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit 16f1a49

Browse files
author
hanhxiao
committed
fix(base): move set config to metaclass
1 parent b97acd6 commit 16f1a49

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

gnes/base/__init__.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,26 @@ def __call__(cls, *args, **kwargs):
7777

7878
obj = type.__call__(cls, *args, **kwargs)
7979

80-
obj._set_gnes_config(**kwargs)
80+
# set attribute
81+
for k, v in TrainableType.default_gnes_config.items():
82+
if k in kwargs:
83+
v = kwargs[k]
84+
setattr(obj, k, v)
85+
86+
if not getattr(obj, 'name', None):
87+
_id = str(uuid.uuid4()).split('-')[0]
88+
_name = '%s-%s' % (obj.__class__.__name__, _id)
89+
obj.logger.warning(
90+
'this object is not named ("- gnes_config: - name" is not found in YAML config), '
91+
'i will call it as "%s". '
92+
'naming the object is important especially when you need to '
93+
'serialize/deserialize/store/load the object.' % _name)
94+
setattr(obj, 'name', _name)
8195

8296
getattr(obj, '_post_init_wrapper', lambda *x: None)()
8397
return obj
8498

99+
85100
@staticmethod
86101
def register_class(cls):
87102
# print('try to register class: %s' % cls.__name__)
@@ -166,22 +181,6 @@ def post_init(self):
166181
def pre_init(cls):
167182
pass
168183

169-
def _set_gnes_config(self, **kwargs):
170-
# set attribute
171-
for k, v in TrainableType.default_gnes_config.items():
172-
if k in kwargs:
173-
v = kwargs[k]
174-
setattr(self, k, v)
175-
176-
if not getattr(self, 'name', None):
177-
_id = str(uuid.uuid4()).split('-')[0]
178-
_name = '%s-%s' % (self.__class__.__name__, _id)
179-
self.logger.warning(
180-
'this object is not named ("- gnes_config: - name" is not found in YAML config), '
181-
'i will call it as "%s". '
182-
'However, naming the object is important especially when you need to '
183-
'serialize/deserialize/store/load the object.' % _name)
184-
setattr(self, 'name', _name)
185184

186185
@property
187186
def dump_full_path(self):

0 commit comments

Comments
 (0)