提交 b23e3ffe 编写于 作者: M michaelowenliu

add a component name, remove null return, fix word typo

上级 87043c8b
...@@ -13,21 +13,25 @@ ...@@ -13,21 +13,25 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from collections.abc import Sequence
import inspect import inspect
from collections.abc import Sequence
class ComponentManager: class ComponentManager:
""" """
Implement a manager class to add the new component properly. Implement a manager class to add the new component properly.
The component can be added as either class or function type. The component can be added as either class or function type.
Args:
name (str): the name of component.
For example: For example:
>>> model_manager = ComponentManager() >>> model_manager = ComponentManager()
>>> class AlexNet: ... >>> class AlexNet: ...
>>> class ResNet: ... >>> class ResNet: ...
>>> model_manager.add_component(AlexNet) >>> model_manager.add_component(AlexNet)
>>> model_manager.add_component(ResNet) >>> model_manager.add_component(ResNet)
or pass a sequence alliteratively: or pass a sequence alternatively:
>>> model_manager.add_component([AlexNet, ResNet]) >>> model_manager.add_component([AlexNet, ResNet])
>>> print(model_manager.components_dict) >>> print(model_manager.components_dict)
output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>} output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
...@@ -42,19 +46,21 @@ class ComponentManager: ...@@ -42,19 +46,21 @@ class ComponentManager:
output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>} output: {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
""" """
def __init__(self): def __init__(self, name=None):
self._components_dict = dict() self._components_dict = dict()
self._name = name
def __len__(self): def __len__(self):
return len(self._components_dict) return len(self._components_dict)
def __repr__(self): def __repr__(self):
return "{}:{}".format(self.__class__.__name__, name_str = self._name if self._name else self.__class__.__name__
return "{}:{}".format(name_str,
list(self._components_dict.keys())) list(self._components_dict.keys()))
def __getitem__(self, item): def __getitem__(self, item):
if item not in self._components_dict.keys(): if item not in self._components_dict.keys():
raise KeyError("{} does not exist in the current {}".format( raise KeyError("{} does not exist in availabel {}".format(
item, self)) item, self))
return self._components_dict[item] return self._components_dict[item]
...@@ -62,15 +68,16 @@ class ComponentManager: ...@@ -62,15 +68,16 @@ class ComponentManager:
def components_dict(self): def components_dict(self):
return self._components_dict return self._components_dict
@property
def name(self):
return self._name
def _add_single_component(self, component): def _add_single_component(self, component):
""" """
Add a single component into the corresponding manager Add a single component into the corresponding manager
Args: Args:
component (function | class): a new component component (function|class): a new component
Returns:
None
""" """
# Currently only support class or function type # Currently only support class or function type
...@@ -94,10 +101,10 @@ class ComponentManager: ...@@ -94,10 +101,10 @@ class ComponentManager:
Add component(s) into the corresponding manager Add component(s) into the corresponding manager
Args: Args:
components (function | class | list | tuple): support three types of components components (function|class|list|tuple): support four types of components
Returns: Returns:
None components (function|class|list|tuple): same with input components
""" """
# Check whether the type is a sequence # Check whether the type is a sequence
...@@ -111,8 +118,8 @@ class ComponentManager: ...@@ -111,8 +118,8 @@ class ComponentManager:
return components return components
MODELS = ComponentManager() MODELS = ComponentManager("models")
BACKBONES = ComponentManager() BACKBONES = ComponentManager("backbones")
DATASETS = ComponentManager() DATASETS = ComponentManager("datasets")
TRANSFORMS = ComponentManager() TRANSFORMS = ComponentManager("transforms")
LOSSES = ComponentManager() LOSSES = ComponentManager("losses")
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册