From b23e3ffee643bc58417d78699ccb11910cb51b20 Mon Sep 17 00:00:00 2001 From: michaelowenliu Date: Mon, 28 Sep 2020 11:05:05 +0800 Subject: [PATCH] add a component name, remove null return, fix word typo --- dygraph/paddleseg/cvlibs/manager.py | 39 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/dygraph/paddleseg/cvlibs/manager.py b/dygraph/paddleseg/cvlibs/manager.py index 33907006..96668c7c 100644 --- a/dygraph/paddleseg/cvlibs/manager.py +++ b/dygraph/paddleseg/cvlibs/manager.py @@ -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': , 'ResNet': } @@ -42,19 +46,21 @@ class ComponentManager: output: {'AlexNet': , '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") -- GitLab