From 6e1c1a01e32f4bfbd7f4e7d12fbbb5c7430714f0 Mon Sep 17 00:00:00 2001 From: michaelowenliu Date: Fri, 24 Jul 2020 17:53:06 +0800 Subject: [PATCH] update manager --- dygraph/cvlibs/manager.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/dygraph/cvlibs/manager.py b/dygraph/cvlibs/manager.py index e268bf23..4d008d66 100644 --- a/dygraph/cvlibs/manager.py +++ b/dygraph/cvlibs/manager.py @@ -28,24 +28,38 @@ class ComponentManager: >>> model_manager.add_component(ResNet) or pass a sequence alliteratively: >>> model_manager.add_component([AlexNet, ResNet]) - >>> print(model_manager.components) + >>> print(model_manager.components_dict) output: {'AlexNet': , '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.add_component >>> class AlexNet: ... >>> @model_manager.add_component >>> class ResNet: ... - >>> print(model_manager.components) + >>> print(model_manager.components_dict) output: {'AlexNet': , 'ResNet': } """ def __init__(self): 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 - def components(self): + def components_dict(self): return self._components_dict def _add_single_component(self, component): @@ -77,17 +91,11 @@ class ComponentManager: :param components (class | list | tuple): support three types of components :return: None """ - + print(components) # check whether the type is a sequence if isinstance(components, collections.Sequence): for component in components: self._add_single_component(component) else: component = components - self._add_single_component(component) - - 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 + self._add_single_component(component) \ No newline at end of file -- GitLab