提交 6e1c1a01 编写于 作者: M michaelowenliu

update manager

上级 cd5e588c
...@@ -28,24 +28,38 @@ class ComponentManager: ...@@ -28,24 +28,38 @@ class ComponentManager:
>>> model_manager.add_component(ResNet) >>> model_manager.add_component(ResNet)
or pass a sequence alliteratively: or pass a sequence alliteratively:
>>> model_manager.add_component([AlexNet, ResNet]) >>> model_manager.add_component([AlexNet, ResNet])
>>> print(model_manager.components) >>> print(model_manager.components_dict)
output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>} output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
Or an easier way, using it as a Python decorator, while just add it above the class declaration. Or a easier way, using it as a Python decorator, while just add it above the class declaration.
>>> model_manager = ComponentManager() >>> model_manager = ComponentManager()
>>> @model_manager.add_component >>> @model_manager.add_component
>>> class AlexNet: ... >>> class AlexNet: ...
>>> @model_manager.add_component >>> @model_manager.add_component
>>> class ResNet: ... >>> class ResNet: ...
>>> print(model_manager.components) >>> print(model_manager.components_dict)
output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>} output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
""" """
def __init__(self): def __init__(self):
self._components_dict = dict() self._components_dict = dict()
def __len__(self):
return len(self._components_dict)
def __repr__(self):
return "{}:{}".format(self.__class__.__name__, list(self._components_dict.keys()))
def __getitem__(self, item):
if item not in self._components_dict.keys():
raise KeyError("{} does not exist in the current {}".format(item, self))
return self._components_dict[item]
def __contains__(self, item):
return self._components_dict.get(item, None) is not None
@property @property
def components(self): def components_dict(self):
return self._components_dict return self._components_dict
def _add_single_component(self, component): def _add_single_component(self, component):
...@@ -77,17 +91,11 @@ class ComponentManager: ...@@ -77,17 +91,11 @@ class ComponentManager:
:param components (class | list | tuple): support three types of components :param components (class | list | tuple): support three types of components
:return: None :return: None
""" """
print(components)
# check whether the type is a sequence # check whether the type is a sequence
if isinstance(components, collections.Sequence): if isinstance(components, collections.Sequence):
for component in components: for component in components:
self._add_single_component(component) self._add_single_component(component)
else: else:
component = components component = components
self._add_single_component(component) self._add_single_component(component)
\ No newline at end of file
def __len__(self):
return len(self._components_dict)
def __repr__(self):
return "{}:{}".format(self.__class__.__name__, list(self._components_dict.keys()))
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册