From 283320de46f4c0f94a54cc46660fae086e14f9d5 Mon Sep 17 00:00:00 2001 From: "Michael (Doc) Norton" Date: Thu, 11 Feb 2010 12:26:19 +0800 Subject: [PATCH] Extract argument conversion to utility class --- tweepy/binder.py | 12 ++---------- tweepy/utils.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/tweepy/binder.py b/tweepy/binder.py index f940f66..8347ebd 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -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): diff --git a/tweepy/utils.py b/tweepy/utils.py index 60a4713..5697045 100644 --- a/tweepy/utils.py +++ b/tweepy/utils.py @@ -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 -- 2.25.1