提交 b23e3ffe 编写于 作者: M michaelowenliu

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

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