From 126305d93db96029b14f234fdc27b42e220e1043 Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Tue, 21 May 2013 12:52:17 -0400 Subject: [PATCH] Revert removing unicode2utf8 and encode staticmethods [ci skip] --- HISTORY.rst | 1 - twython/helpers.py | 8 +------- twython/twython.py | 21 ++++++++++++++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 906b2cb..1a137a6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -10,7 +10,6 @@ History - Callback URL is optional in ``get_authentication_tokens`` to accomedate those using OOB authorization (non web clients) - Not part of the python package, but tests are now available along with Travis CI hooks - Added ``__repr__`` definition for Twython, when calling only returning -- Removed ``Twython.unicode2utf8`` and ``Twython.encode`` methods - Cleaned up ``Twython.construct_api_url``, uses "transparent" parameters (see 4th bullet in this version for explaination) 2.9.1 (2013-05-04) diff --git a/twython/helpers.py b/twython/helpers.py index 76ca404..7b8275b 100644 --- a/twython/helpers.py +++ b/twython/helpers.py @@ -1,4 +1,4 @@ -from .compat import basestring, is_py2, str +from .compat import basestring def _transparent_params(_params): @@ -17,9 +17,3 @@ def _transparent_params(_params): else: continue return params, files - - -def _encode(value): - if is_py2 and isinstance(value, str): - value.encode('utf-8') - return value diff --git a/twython/twython.py b/twython/twython.py index c877c5d..54c2979 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -6,10 +6,10 @@ from requests_oauthlib import OAuth1 from . import __version__ from .advisory import TwythonDeprecationWarning -from .compat import json, urlencode, parse_qsl, quote_plus, str +from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2 from .endpoints import api_table from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError -from .helpers import _encode, _transparent_params +from .helpers import _transparent_params warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 > @@ -354,7 +354,7 @@ class Twython(object): params = requests.utils.to_key_val_list(params) for (k, v) in params: querystring.append( - '%s=%s' % (_encode(k), quote_plus(_encode(v))) + '%s=%s' % (Twython.encode(k), quote_plus(Twython.encode(v))) ) return '%s?%s' % (base_url, '&'.join(querystring)) @@ -393,3 +393,18 @@ class Twython(object): for tweet in self.searchGen(search_query, **kwargs): yield tweet + + @staticmethod + def unicode2utf8(text): + try: + if is_py2 and isinstance(text, str): + text = text.encode('utf-8') + except: + pass + return text + + @staticmethod + def encode(text): + if is_py2 and isinstance(text, (str)): + return Twython.unicode2utf8(text) + return str(text)