Skip to content

Commit f85e4f2

Browse files
committed
simplifies instance checking for iterators, cleans up naming and docs for
models.
1 parent faa3ff6 commit f85e4f2

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

twitter/models.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ def AsDict(self):
3636
for (key, value) in self.param_defaults.items():
3737

3838
# If the value is a list, we need to create a list to hold the
39-
# dicts created by the object supporting the AsDict() method,
39+
# dicts created by an object supporting the AsDict() method,
4040
# i.e., if it inherits from TwitterModel. If the item in the list
4141
# doesn't support the AsDict() method, then we assign the value
4242
# directly.
43-
if isinstance(getattr(self, key, None), list) or \
44-
isinstance(getattr(self, key, None), tuple) or \
45-
isinstance(getattr(self, key, None), set):
43+
if isinstance(getattr(self, key, None), (list, tuple, set)):
4644
data[key] = list()
4745
for subobj in getattr(self, key, None):
4846
if getattr(subobj, 'AsDict', None):
@@ -65,13 +63,12 @@ def AsDict(self):
6563

6664
@classmethod
6765
def NewFromJsonDict(cls, data, **kwargs):
68-
""" Create a new instance based on a JSON dict.
66+
""" Create a new instance based on a JSON dict. Any kwargs should be
67+
supplied by the inherited, calling class.
6968
7069
Args:
71-
data: A JSON dict, as converted from the JSON in the twitter API
70+
data: A JSON dict, as converted from the JSON in the twitter API.
7271
73-
Returns:
74-
A twitter.Media instance
7572
"""
7673

7774
if kwargs:
@@ -100,9 +97,9 @@ def __init__(self, **kwargs):
10097
setattr(self, param, kwargs.get(param, default))
10198

10299
def __repr__(self):
103-
return "Media(ID={media_id}, Type={type}, DisplayURL='{url}')".format(
100+
return "Media(ID={media_id}, Type={media_type}, DisplayURL='{url}')".format(
104101
media_id=self.id,
105-
type=self.type,
102+
media_type=self.type,
106103
url=self.display_url)
107104

108105

@@ -285,11 +282,11 @@ def __init__(self, **kwargs):
285282
setattr(self, param, True)
286283

287284
def __repr__(self):
288-
conns = [param for param in self.connections if getattr(self, param)]
285+
connections = [param for param in self.connections if getattr(self, param)]
289286
return "UserStatus(ID={uid}, ScreenName={sn}, Connections=[{conn}])".format(
290287
uid=self.id,
291288
sn=self.screen_name,
292-
conn=", ".join(conns))
289+
conn=", ".join(connections))
293290

294291

295292
class User(TwitterModel):
@@ -469,7 +466,6 @@ def NewFromJsonDict(data):
469466
A twitter.Status instance
470467
"""
471468
if 'user' in data:
472-
from twitter import User
473469
user = User.NewFromJsonDict(data['user'])
474470
else:
475471
user = None
@@ -494,7 +490,6 @@ def NewFromJsonDict(data):
494490
urls = [Url.NewFromJsonDict(u) for u in data['entities']['urls']]
495491

496492
if 'user_mentions' in data['entities']:
497-
from twitter import User
498493
user_mentions = [User.NewFromJsonDict(u) for u in data['entities']['user_mentions']]
499494

500495
if 'hashtags' in data['entities']:

0 commit comments

Comments
 (0)