[tor-commits] [stem/master] Remove ugly if-elif tree in favour of a dict

atagar at torproject.org atagar at torproject.org
Mon Jul 9 18:41:26 UTC 2012


commit c5472856c9895bd118af79297ccdce59fcba2e13
Author: Ravi Chandra Padmala <neenaoffline at gmail.com>
Date:   Mon Jul 9 10:12:23 2012 +0530

    Remove ugly if-elif tree in favour of a dict
---
 stem/response/__init__.py |   23 ++++++++++++-----------
 1 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/stem/response/__init__.py b/stem/response/__init__.py
index 995cc69..2d7f028 100644
--- a/stem/response/__init__.py
+++ b/stem/response/__init__.py
@@ -84,17 +84,18 @@ def convert(response_type, message, **kwargs):
   if not isinstance(message, ControlMessage):
     raise TypeError("Only able to convert stem.response.ControlMessage instances")
   
-  if response_type == "GETINFO":
-    response_class = stem.response.getinfo.GetInfoResponse
-  elif response_type == "GETCONF":
-    response_class = stem.response.getconf.GetConfResponse
-  elif response_type == "SINGLELINE":
-    response_class = SingleLineResponse
-  elif response_type == "PROTOCOLINFO":
-    response_class = stem.response.protocolinfo.ProtocolInfoResponse
-  elif response_type == "AUTHCHALLENGE":
-    response_class = stem.response.authchallenge.AuthChallengeResponse
-  else: raise TypeError("Unsupported response type: %s" % response_type)
+  response_types = {
+    "GETINFO": stem.response.getinfo.GetInfoResponse,
+    "GETCONF": stem.response.getconf.GetConfResponse,
+    "SINGLELINE": SingleLineResponse,
+    "PROTOCOLINFO": stem.response.protocolinfo.ProtocolInfoResponse,
+    "AUTHCHALLENGE": stem.response.authchallenge.AuthChallengeResponse,
+    }
+  
+  try:
+    response_class = response_types[response_type]
+  except TypeError:
+    raise TypeError("Unsupported response type: %s" % response_type)
   
   message.__class__ = response_class
   message._parse_message(**kwargs)





More information about the tor-commits mailing list