From 76c9a16d23a007bb7c91bb8cb07844e6b717ac10 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Mon, 17 Dec 2012 18:07:04 +0900 Subject: don't abuse __dict__ attribute There are built-in functions to handle attribute like hasattr. Use standard functions instead of abusing __dict__. Signed-off-by: Isaku Yamahata Signed-off-by: FUJITA Tomonori --- ryu/base/app_manager.py | 13 ++++++------- ryu/controller/handler.py | 2 +- ryu/controller/ofp_event.py | 6 ++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/ryu/base/app_manager.py b/ryu/base/app_manager.py index 5a35a6e6..4a29c95d 100644 --- a/ryu/base/app_manager.py +++ b/ryu/base/app_manager.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import inspect import itertools import logging @@ -72,12 +73,10 @@ class AppManager(object): def load_app(self, name): mod = utils.import_module(name) - for k, v in mod.__dict__.items(): - try: - if issubclass(v, RyuApp): - return getattr(mod, k) - except TypeError: - pass + clses = inspect.getmembers(mod, lambda cls: (inspect.isclass(cls) and + issubclass(cls, RyuApp))) + if clses: + return clses[0][1] return None def load_apps(self, app_lists): @@ -113,7 +112,7 @@ class AppManager(object): # Yes, maybe for slicing. LOG.info('instantiating app %s', app_name) - if 'OFP_VERSIONS' in cls.__dict__: + if hasattr(cls, 'OFP_VERSIONS'): for k in Datapath.supported_ofp_version.keys(): if not k in cls.OFP_VERSIONS: del Datapath.supported_ofp_version[k] diff --git a/ryu/controller/handler.py b/ryu/controller/handler.py index 4566cd5a..ede27df0 100644 --- a/ryu/controller/handler.py +++ b/ryu/controller/handler.py @@ -44,7 +44,7 @@ def set_ev_cls(ev_cls, dispatchers): def _is_ev_handler(meth): - return 'ev_cls' in meth.__dict__ + return hasattr(meth, 'ev_cls') def _listify(may_list): diff --git a/ryu/controller/ofp_event.py b/ryu/controller/ofp_event.py index ed9f1b38..e5becac0 100644 --- a/ryu/controller/ofp_event.py +++ b/ryu/controller/ofp_event.py @@ -59,10 +59,8 @@ def _create_ofp_msg_ev_class(msg_cls): def _create_ofp_msg_ev_from_module(modname): mod = utils.import_module(modname) # print mod - for _k, cls in mod.__dict__.items(): - if not inspect.isclass(cls): - continue - if 'cls_msg_type' not in cls.__dict__: + for _k, cls in inspect.getmembers(mod, inspect.isclass): + if not hasattr(cls, 'cls_msg_type'): continue _create_ofp_msg_ev_class(cls) -- cgit v1.2.3