PEP8 Cleanup

Attempting to clean up some code.
* There were a lot of libraries not being used any longer
* Changed some vars named "id" to "id_", id is a reserved var
This commit is contained in:
Michael Helmick 2012-02-24 12:36:22 -05:00
parent ad81c73923
commit 95f7abc7a3

View file

@ -11,14 +11,9 @@
__author__ = "Ryan McGrath <ryan@venodesigns.net>" __author__ = "Ryan McGrath <ryan@venodesigns.net>"
__version__ = "1.4.6" __version__ = "1.4.6"
import cgi
import urllib import urllib
import urllib2 import urllib2
import urlparse
import httplib
import httplib2 import httplib2
import mimetypes
import mimetools
import re import re
import inspect import inspect
import time import time
@ -69,6 +64,7 @@ if not hasattr(oauth, '_version') or float(oauth._version.manual_verstr) <= 1.4:
else: else:
OAUTH_CALLBACK_IN_URL = True OAUTH_CALLBACK_IN_URL = True
class TwythonError(AttributeError): class TwythonError(AttributeError):
""" """
Generic error class, catch-all for most Twython issues. Generic error class, catch-all for most Twython issues.
@ -100,6 +96,7 @@ class TwythonAPILimit(TwythonError):
def __str__(self): def __str__(self):
return repr(self.msg) return repr(self.msg)
class TwythonRateLimitError(TwythonError): class TwythonRateLimitError(TwythonError):
""" """
Raised when you've hit a rate limit. retry_wait_seconds is the number of seconds to Raised when you've hit a rate limit. retry_wait_seconds is the number of seconds to
@ -126,7 +123,8 @@ class TwythonAuthError(TwythonError):
class Twython(object): class Twython(object):
def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None, client_args={}): def __init__(self, twitter_token=None, twitter_secret=None, oauth_token=None, oauth_token_secret=None, \
headers=None, callback_url=None, client_args=None):
"""setup(self, oauth_token = None, headers = None) """setup(self, oauth_token = None, headers = None)
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below). Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
@ -140,7 +138,8 @@ class Twython(object):
headers - User agent header, dictionary style ala {'User-Agent': 'Bert'} headers - User agent header, dictionary style ala {'User-Agent': 'Bert'}
client_args - additional arguments for HTTP client (see httplib2.Http.__init__), e.g. {'timeout': 10.0} client_args - additional arguments for HTTP client (see httplib2.Http.__init__), e.g. {'timeout': 10.0}
** Note: versioning is not currently used by search.twitter functions; when Twitter moves their junk, it'll be supported. ** Note: versioning is not currently used by search.twitter functions;
when Twitter moves their junk, it'll be supported.
""" """
# Needed for hitting that there API. # Needed for hitting that there API.
self.request_token_url = 'http://twitter.com/oauth/request_token' self.request_token_url = 'http://twitter.com/oauth/request_token'
@ -161,6 +160,8 @@ class Twython(object):
self.consumer = None self.consumer = None
self.token = None self.token = None
client_args = client_args or {}
if self.twitter_token is not None and self.twitter_secret is not None: if self.twitter_token is not None and self.twitter_secret is not None:
self.consumer = oauth.Consumer(self.twitter_token, self.twitter_secret) self.consumer = oauth.Consumer(self.twitter_token, self.twitter_secret)
@ -176,6 +177,7 @@ class Twython(object):
# If they don't do authentication, but still want to request unprotected resources, we need an opener. # If they don't do authentication, but still want to request unprotected resources, we need an opener.
self.client = httplib2.Http(**client_args) self.client = httplib2.Http(**client_args)
# register available funcs to allow listing name when debugging. # register available funcs to allow listing name when debugging.
def setFunc(key): def setFunc(key):
return lambda **kwargs: self._constructFunc(key, **kwargs) return lambda **kwargs: self._constructFunc(key, **kwargs)
for key in api_table.keys(): for key in api_table.keys():
@ -275,7 +277,7 @@ class Twython(object):
content = urllib2.urlopen(shortener + "?" + urllib.urlencode({query: Twython.unicode2utf8(url_to_shorten)})).read() content = urllib2.urlopen(shortener + "?" + urllib.urlencode({query: Twython.unicode2utf8(url_to_shorten)})).read()
return content return content
except HTTPError, e: except HTTPError, e:
raise TwythonError("shortenURL() failed with a %s error code." % `e.code`) raise TwythonError("shortenURL() failed with a %s error code." % e.code)
def bulkUserLookup(self, ids=None, screen_names=None, version=1, **kwargs): def bulkUserLookup(self, ids=None, screen_names=None, version=1, **kwargs):
""" bulkUserLookup(self, ids = None, screen_names = None, version = 1, **kwargs) """ bulkUserLookup(self, ids = None, screen_names = None, version = 1, **kwargs)
@ -295,7 +297,7 @@ class Twython(object):
resp, content = self.client.request(lookupURL, "POST", headers=self.headers) resp, content = self.client.request(lookupURL, "POST", headers=self.headers)
return simplejson.loads(content.decode('utf-8')) return simplejson.loads(content.decode('utf-8'))
except HTTPError, e: except HTTPError, e:
raise TwythonError("bulkUserLookup() failed with a %s error code." % `e.code`, e.code) raise TwythonError("bulkUserLookup() failed with a %s error code." % e.code, e.code)
def search(self, **kwargs): def search(self, **kwargs):
"""search(search_query, **kwargs) """search(search_query, **kwargs)
@ -320,7 +322,7 @@ class Twython(object):
return simplejson.loads(content.decode('utf-8')) return simplejson.loads(content.decode('utf-8'))
except HTTPError, e: except HTTPError, e:
raise TwythonError("getSearchTimeline() failed with a %s error code." % `e.code`, e.code) raise TwythonError("getSearchTimeline() failed with a %s error code." % e.code, e.code)
def searchTwitter(self, **kwargs): def searchTwitter(self, **kwargs):
"""use search() ,this is a fall back method to support searchTwitter() """use search() ,this is a fall back method to support searchTwitter()
@ -343,7 +345,7 @@ class Twython(object):
resp, content = self.client.request(searchURL, "GET", headers=self.headers) resp, content = self.client.request(searchURL, "GET", headers=self.headers)
data = simplejson.loads(content.decode('utf-8')) data = simplejson.loads(content.decode('utf-8'))
except HTTPError, e: except HTTPError, e:
raise TwythonError("searchGen() failed with a %s error code." % `e.code`, e.code) raise TwythonError("searchGen() failed with a %s error code." % e.code, e.code)
if not data['results']: if not data['results']:
raise StopIteration raise StopIteration
@ -362,7 +364,7 @@ class Twython(object):
raise TwythonError("searchGen() exited because page takes str") raise TwythonError("searchGen() exited because page takes str")
except e: except e:
raise TwythonError("searchGen() failed with %s error code" % \ raise TwythonError("searchGen() failed with %s error code" % \
`e.code`, e.code) e.code, e.code)
for tweet in self.searchGen(search_query, **kwargs): for tweet in self.searchGen(search_query, **kwargs):
yield tweet yield tweet
@ -372,7 +374,7 @@ class Twython(object):
searchTwitterGen()""" searchTwitterGen()"""
return self.searchGen(search_query, **kwargs) return self.searchGen(search_query, **kwargs)
def isListMember(self, list_id, id, username, version = 1): def isListMember(self, list_id, id_, username, version=1):
""" isListMember(self, list_id, id, version) """ isListMember(self, list_id, id, version)
Check if a specified user (id) is a member of the list in question (list_id). Check if a specified user (id) is a member of the list in question (list_id).
@ -386,12 +388,12 @@ class Twython(object):
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc. version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
""" """
try: try:
resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/members/%s.json" % (version, username, list_id, `id`), headers = self.headers) resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/members/%s.json" % (version, username, list_id, id_), headers=self.headers)
return simplejson.loads(content.decode('utf-8')) return simplejson.loads(content.decode('utf-8'))
except HTTPError, e: except HTTPError, e:
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code) raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
def isListSubscriber(self, username, list_id, id, version = 1): def isListSubscriber(self, username, list_id, id_, version=1):
""" isListSubscriber(self, list_id, id, version) """ isListSubscriber(self, list_id, id, version)
Check if a specified user (id) is a subscriber of the list in question (list_id). Check if a specified user (id) is a subscriber of the list in question (list_id).
@ -405,7 +407,7 @@ class Twython(object):
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc. version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
""" """
try: try:
resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/following/%s.json" % (version, username, list_id, `id`), headers = self.headers) resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/following/%s.json" % (version, username, list_id, id_), headers=self.headers)
return simplejson.loads(content.decode('utf-8')) return simplejson.loads(content.decode('utf-8'))
except HTTPError, e: except HTTPError, e:
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code) raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
@ -466,7 +468,6 @@ class Twython(object):
req = requests.post(url, data=params, files=file_, headers=self.headers) req = requests.post(url, data=params, files=file_, headers=self.headers)
return req.content return req.content
def getProfileImageUrl(self, username, size=None, version=1): def getProfileImageUrl(self, username, size=None, version=1):
""" getProfileImageUrl(username) """ getProfileImageUrl(username)