From c7bce9189fa6291430b4cbf30ac9497dd11fb3db Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Tue, 21 May 2013 11:41:43 -0400 Subject: [PATCH] Update requests dependency, add str py2/3 compat, __repr__ definition, removed unicode2utf8 & encode static methods, update HISTORY @ryanmcgrath Let me know if you're okay with the removal of Twython.unicode2utf8 and Twython.encode. I moved Twython.encode to _encode in helpers.py (only place being used is Twython.construct_api_url) If it's python 2 and unicode then we encode it, otherwise return the original value [ci skip] --- HISTORY.rst | 3 +++ requirements.txt | 2 +- setup.py | 2 +- twython/compat.py | 2 ++ twython/helpers.py | 8 +++++++- twython/twython.py | 31 +++++++++++++------------------ 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c824ff2..906b2cb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,6 +9,9 @@ History - Added "transparent" parameters for making requests, meaning users can pass bool values (True, False) to Twython methods and we convert your params in the background to satisfy the Twitter API. Also, file objects can now be passed seamlessly (see examples in README and in /examples dir for details) - 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/requirements.txt b/requirements.txt index 70bd0e7..edc9ff6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ coverage==3.6.0 -requests==1.2.0 +requests==1.2.1 requests_oauthlib==0.3.1 diff --git a/setup.py b/setup.py index 42de4b2..733e775 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setup( include_package_data=True, # Package dependencies. - install_requires=['requests==1.2.0', 'requests_oauthlib==0.3.1'], + install_requires=['requests==1.2.1', 'requests_oauthlib==0.3.1'], # Metadata for PyPI. author='Ryan McGrath', diff --git a/twython/compat.py b/twython/compat.py index e44b5b4..79f9c2c 100644 --- a/twython/compat.py +++ b/twython/compat.py @@ -20,10 +20,12 @@ if is_py2: except ImportError: from cgi import parse_qsl + str = unicode basestring = basestring elif is_py3: from urllib.parse import urlencode, quote_plus, parse_qsl + str = str basestring = (str, bytes) diff --git a/twython/helpers.py b/twython/helpers.py index 7b8275b..76ca404 100644 --- a/twython/helpers.py +++ b/twython/helpers.py @@ -1,4 +1,4 @@ -from .compat import basestring +from .compat import basestring, is_py2, str def _transparent_params(_params): @@ -17,3 +17,9 @@ 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 af1e159..c877c5d 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 +from .compat import json, urlencode, parse_qsl, quote_plus, str from .endpoints import api_table from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError -from .helpers import _transparent_params +from .helpers import _encode, _transparent_params warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 > @@ -106,6 +106,9 @@ class Twython(object): # create stash for last call intel self._last_call = None + def __repr__(self): + return '' % (self.app_key) + def _constructFunc(self, api_call, deprecated_key, **kwargs): # Go through and replace any mustaches that are in our API url. fn = api_table[api_call] @@ -346,7 +349,14 @@ class Twython(object): @staticmethod def construct_api_url(base_url, params): - return base_url + '?' + '&'.join(['%s=%s' % (Twython.unicode2utf8(key), quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()]) + querystring = [] + params, _ = _transparent_params(params) + params = requests.utils.to_key_val_list(params) + for (k, v) in params: + querystring.append( + '%s=%s' % (_encode(k), quote_plus(_encode(v))) + ) + return '%s?%s' % (base_url, '&'.join(querystring)) def searchGen(self, search_query, **kwargs): warnings.warn( @@ -383,18 +393,3 @@ class Twython(object): for tweet in self.searchGen(search_query, **kwargs): yield tweet - - @staticmethod - def unicode2utf8(text): - try: - if isinstance(text, unicode): - text = text.encode('utf-8') - except: - pass - return text - - @staticmethod - def encode(text): - if isinstance(text, (str, unicode)): - return Twython.unicode2utf8(text) - return str(text)