Extract argument conversion to utility class
authorMichael (Doc) Norton <michael@docondev.com>
Thu, 11 Feb 2010 04:26:19 +0000 (12:26 +0800)
committerJoshua Roesslein <jroesslein@gmail.com>
Thu, 11 Feb 2010 04:35:59 +0000 (12:35 +0800)
tweepy/binder.py
tweepy/utils.py

index f940f669ac4f2c64733ec721c4e9ab6a2047f5d7..8347ebd67cdb28b7a666c4a59cb2509e922a4af1 100644 (file)
@@ -66,13 +66,9 @@ def bind_api(**config):
         def build_parameters(self, args, kargs):
             self.parameters = {}
             for idx, arg in enumerate(args):
-                if isinstance(arg, unicode):
-                    arg = arg.encode('utf-8')
-                elif not isinstance(arg, str):
-                    arg = str(arg)
 
                 try:
-                    self.parameters[self.allowed_param[idx]] = arg
+                    self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
                 except IndexError:
                     raise TweepError('Too many parameters supplied!')
 
@@ -82,11 +78,7 @@ def bind_api(**config):
                 if k in self.parameters:
                     raise TweepError('Multiple values for parameter %s supplied!' % k)
 
-                if isinstance(arg, unicode):
-                    arg = arg.encode('utf-8')
-                elif not isinstance(arg, str):
-                    arg = str(arg)
-                self.parameters[k] = arg
+                self.parameters[k] = convert_to_utf8_str(arg)
 
         def build_path(self):
             for variable in re_path_template.findall(self.path):
index 60a4713044f0bc41d130451e44a018d43fa06f72..56970454b1eb367368fde2b268b232eed8793a71 100644 (file)
@@ -55,6 +55,16 @@ def unescape_html(text):
     return re.sub("&#?\w+;", fixup, text)
 
 
+def convert_to_utf8_str(arg):
+    # written by Michael Norton (http://docondev.blogspot.com/)
+    if isinstance(arg, unicode):
+        arg = arg.encode('utf-8')
+    elif not isinstance(arg, str):
+        arg = str(arg)
+    return arg
+
+
+
 def import_simplejson():
     try:
         import simplejson as json