X Tutup
Skip to content

Commit 35bd861

Browse files
committed
added code style fixes to conform with PEP8
1 parent 12f7e9f commit 35bd861

File tree

2 files changed

+144
-141
lines changed

2 files changed

+144
-141
lines changed

twitter/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
'''A library that provides a Python interface to the Twitter API'''
19+
"""A library that provides a Python interface to the Twitter API"""
2020

2121
__author__ = 'python-twitter@googlegroups.com'
2222
__version__ = '2.3'
2323

2424
import json as simplejson
2525

2626
try:
27-
from hashlib import md5
27+
from hashlib import md5
2828
except ImportError:
29-
from md5 import md5
29+
from md5 import md5
3030

3131
from _file_cache import _FileCache
3232
from error import TwitterError

twitter/_file_cache.py

Lines changed: 141 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -4,147 +4,150 @@
44
import re
55
import tempfile
66

7+
78
class _FileCacheError(Exception):
8-
'''Base exception class for FileCache related errors'''
9+
"""Base exception class for FileCache related errors"""
10+
911

1012
class _FileCache(object):
13+
DEPTH = 3
14+
15+
def __init__(self, root_directory=None):
16+
self._InitializeRootDirectory(root_directory)
17+
18+
def Get(self, key):
19+
path = self._GetPath(key)
20+
if os.path.exists(path):
21+
return open(path).read()
22+
else:
23+
return None
24+
25+
def Set(self, key, data):
26+
path = self._GetPath(key)
27+
directory = os.path.dirname(path)
28+
if not os.path.exists(directory):
29+
os.makedirs(directory)
30+
if not os.path.isdir(directory):
31+
raise _FileCacheError('%s exists but is not a directory' % directory)
32+
temp_fd, temp_path = tempfile.mkstemp()
33+
temp_fp = os.fdopen(temp_fd, 'w')
34+
temp_fp.write(data)
35+
temp_fp.close()
36+
if not path.startswith(self._root_directory):
37+
raise _FileCacheError('%s does not appear to live under %s' %
38+
(path, self._root_directory))
39+
if os.path.exists(path):
40+
os.remove(path)
41+
os.rename(temp_path, path)
42+
43+
def Remove(self, key):
44+
path = self._GetPath(key)
45+
if not path.startswith(self._root_directory):
46+
raise _FileCacheError('%s does not appear to live under %s' %
47+
(path, self._root_directory ))
48+
if os.path.exists(path):
49+
os.remove(path)
50+
51+
def GetCachedTime(self, key):
52+
path = self._GetPath(key)
53+
if os.path.exists(path):
54+
return os.path.getmtime(path)
55+
else:
56+
return None
57+
58+
def _GetUsername(self):
59+
"""Attempt to find the username in a cross-platform fashion."""
60+
try:
61+
return os.getenv('USER') or \
62+
os.getenv('LOGNAME') or \
63+
os.getenv('USERNAME') or \
64+
os.getlogin() or \
65+
'nobody'
66+
except (AttributeError, IOError, OSError), e:
67+
return 'nobody'
68+
69+
def _GetTmpCachePath(self):
70+
username = self._GetUsername()
71+
cache_directory = 'python.cache_' + username
72+
return os.path.join(tempfile.gettempdir(), cache_directory)
73+
74+
def _InitializeRootDirectory(self, root_directory):
75+
if not root_directory:
76+
root_directory = self._GetTmpCachePath()
77+
root_directory = os.path.abspath(root_directory)
78+
if not os.path.exists(root_directory):
79+
os.mkdir(root_directory)
80+
if not os.path.isdir(root_directory):
81+
raise _FileCacheError('%s exists but is not a directory' %
82+
root_directory)
83+
self._root_directory = root_directory
84+
85+
def _GetPath(self, key):
86+
try:
87+
hashed_key = md5(key).hexdigest()
88+
except TypeError:
89+
hashed_key = md5.new(key).hexdigest()
90+
91+
return os.path.join(self._root_directory,
92+
self._GetPrefix(hashed_key),
93+
hashed_key)
94+
95+
def _GetPrefix(self, hashed_key):
96+
return os.path.sep.join(hashed_key[0:_FileCache.DEPTH])
1197

12-
DEPTH = 3
13-
14-
def __init__(self,root_directory=None):
15-
self._InitializeRootDirectory(root_directory)
16-
17-
def Get(self, key):
18-
path = self._GetPath(key)
19-
if os.path.exists(path):
20-
return open(path).read()
21-
else:
22-
return None
23-
24-
def Set(self, key, data):
25-
path = self._GetPath(key)
26-
directory = os.path.dirname(path)
27-
if not os.path.exists(directory):
28-
os.makedirs(directory)
29-
if not os.path.isdir(directory):
30-
raise _FileCacheError('%s exists but is not a directory' % directory)
31-
temp_fd, temp_path = tempfile.mkstemp()
32-
temp_fp = os.fdopen(temp_fd, 'w')
33-
temp_fp.write(data)
34-
temp_fp.close()
35-
if not path.startswith(self._root_directory):
36-
raise _FileCacheError('%s does not appear to live under %s' %
37-
(path, self._root_directory))
38-
if os.path.exists(path):
39-
os.remove(path)
40-
os.rename(temp_path, path)
41-
42-
def Remove(self, key):
43-
path = self._GetPath(key)
44-
if not path.startswith(self._root_directory):
45-
raise _FileCacheError('%s does not appear to live under %s' %
46-
(path, self._root_directory ))
47-
if os.path.exists(path):
48-
os.remove(path)
49-
50-
def GetCachedTime(self, key):
51-
path = self._GetPath(key)
52-
if os.path.exists(path):
53-
return os.path.getmtime(path)
54-
else:
55-
return None
56-
57-
def _GetUsername(self):
58-
'''Attempt to find the username in a cross-platform fashion.'''
59-
try:
60-
return os.getenv('USER') or \
61-
os.getenv('LOGNAME') or \
62-
os.getenv('USERNAME') or \
63-
os.getlogin() or \
64-
'nobody'
65-
except (AttributeError, IOError, OSError), e:
66-
return 'nobody'
67-
68-
def _GetTmpCachePath(self):
69-
username = self._GetUsername()
70-
cache_directory = 'python.cache_' + username
71-
return os.path.join(tempfile.gettempdir(), cache_directory)
72-
73-
def _InitializeRootDirectory(self, root_directory):
74-
if not root_directory:
75-
root_directory = self._GetTmpCachePath()
76-
root_directory = os.path.abspath(root_directory)
77-
if not os.path.exists(root_directory):
78-
os.mkdir(root_directory)
79-
if not os.path.isdir(root_directory):
80-
raise _FileCacheError('%s exists but is not a directory' %
81-
root_directory)
82-
self._root_directory = root_directory
83-
84-
def _GetPath(self, key):
85-
try:
86-
hashed_key = md5(key).hexdigest()
87-
except TypeError:
88-
hashed_key = md5.new(key).hexdigest()
89-
90-
return os.path.join(self._root_directory,
91-
self._GetPrefix(hashed_key),
92-
hashed_key)
93-
94-
def _GetPrefix(self, hashed_key):
95-
return os.path.sep.join(hashed_key[0:_FileCache.DEPTH])
9698

9799
class ParseTweet:
98-
# compile once on import
99-
regexp = { "RT": "^RT", "MT":r"^MT", "ALNUM": r"(@[a-zA-Z0-9_]+)",
100-
"HASHTAG": r"(#[\w\d]+)", "URL": r"([http://]?[a-zA-Z\d\/]+[\.]+[a-zA-Z\d\/\.]+)" }
101-
regexp = dict((key,re.compile(value)) for key,value in regexp.items())
102-
103-
def __init__(self,timeline_owner,tweet):
104-
''' timeline_owner : twitter handle of user account. tweet - 140 chars from feed; object does all computation on construction
105-
properties:
106-
RT, MT - boolean
107-
URLs - list of URL
108-
Hashtags - list of tags
109-
'''
110-
self.Owner = timeline_owner
111-
self.tweet = tweet
112-
self.UserHandles = ParseTweet.getUserHandles(tweet)
113-
self.Hashtags = ParseTweet.getHashtags(tweet)
114-
self.URLs = ParseTweet.getURLs(tweet)
115-
self.RT = ParseTweet.getAttributeRT(tweet)
116-
self.MT = ParseTweet.getAttributeMT(tweet)
117-
118-
# additional intelligence
119-
if ( self.RT and len(self.UserHandles) > 0 ): #change the owner of tweet?
120-
self.Owner = self.UserHandles[0]
121-
return
122-
123-
def __str__(self):
124-
''' for display method '''
125-
return "owner %s, urls: %d, hashtags %d, user_handles %d, len_tweet %d, RT = %s, MT = %s"%(self.Owner,len(self.URLs),len(self.Hashtags),len(self.UserHandles), len(self.tweet), self.RT,self.MT)
126-
127-
@staticmethod
128-
def getAttributeRT( tweet ):
129-
""" see if tweet is a RT """
130-
return re.search(ParseTweet.regexp["RT"],tweet.strip()) != None
131-
132-
@staticmethod
133-
def getAttributeMT( tweet ):
134-
""" see if tweet is a MT """
135-
return re.search(ParseTweet.regexp["MT"],tweet.strip()) != None
136-
137-
@staticmethod
138-
def getUserHandles( tweet ):
139-
""" given a tweet we try and extract all user handles in order of occurrence"""
140-
return re.findall(ParseTweet.regexp["ALNUM"],tweet)
141-
142-
@staticmethod
143-
def getHashtags( tweet ):
144-
""" return all hashtags"""
145-
return re.findall(ParseTweet.regexp["HASHTAG"],tweet)
146-
147-
@staticmethod
148-
def getURLs( tweet ):
149-
""" URL : [http://]?[\w\.?/]+"""
150-
return re.findall(ParseTweet.regexp["URL"],tweet)
100+
# compile once on import
101+
regexp = {"RT": "^RT", "MT": r"^MT", "ALNUM": r"(@[a-zA-Z0-9_]+)",
102+
"HASHTAG": r"(#[\w\d]+)", "URL": r"([http://]?[a-zA-Z\d\/]+[\.]+[a-zA-Z\d\/\.]+)"}
103+
regexp = dict((key, re.compile(value)) for key, value in regexp.items())
104+
105+
def __init__(self, timeline_owner, tweet):
106+
""" timeline_owner : twitter handle of user account. tweet - 140 chars from feed; object does all computation on construction
107+
properties:
108+
RT, MT - boolean
109+
URLs - list of URL
110+
Hashtags - list of tags
111+
"""
112+
self.Owner = timeline_owner
113+
self.tweet = tweet
114+
self.UserHandles = ParseTweet.getUserHandles(tweet)
115+
self.Hashtags = ParseTweet.getHashtags(tweet)
116+
self.URLs = ParseTweet.getURLs(tweet)
117+
self.RT = ParseTweet.getAttributeRT(tweet)
118+
self.MT = ParseTweet.getAttributeMT(tweet)
119+
120+
# additional intelligence
121+
if ( self.RT and len(self.UserHandles) > 0 ): # change the owner of tweet?
122+
self.Owner = self.UserHandles[0]
123+
return
124+
125+
def __str__(self):
126+
""" for display method """
127+
return "owner %s, urls: %d, hashtags %d, user_handles %d, len_tweet %d, RT = %s, MT = %s" % (
128+
self.Owner, len(self.URLs), len(self.Hashtags), len(self.UserHandles), len(self.tweet), self.RT, self.MT)
129+
130+
@staticmethod
131+
def getAttributeRT(tweet):
132+
""" see if tweet is a RT """
133+
return re.search(ParseTweet.regexp["RT"], tweet.strip()) != None
134+
135+
@staticmethod
136+
def getAttributeMT(tweet):
137+
""" see if tweet is a MT """
138+
return re.search(ParseTweet.regexp["MT"], tweet.strip()) != None
139+
140+
@staticmethod
141+
def getUserHandles(tweet):
142+
""" given a tweet we try and extract all user handles in order of occurrence"""
143+
return re.findall(ParseTweet.regexp["ALNUM"], tweet)
144+
145+
@staticmethod
146+
def getHashtags(tweet):
147+
""" return all hashtags"""
148+
return re.findall(ParseTweet.regexp["HASHTAG"], tweet)
149+
150+
@staticmethod
151+
def getURLs(tweet):
152+
""" URL : [http://]?[\w\.?/]+"""
153+
return re.findall(ParseTweet.regexp["URL"], tweet)

0 commit comments

Comments
 (0)
X Tutup