http://api.stackoverflow.com/1.1/users/string_id returns
{
"error": {
"code": 404,
"message": "The server has not found anything matching the Request-URI."
}
}
and will raise KeyError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/9924 returns
{
"total": 0,
"page": 1,
"pagesize": 30,
"users": []
}
and will raise IndexError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
And since you are using userId
as an argument to every method, and the StackOverflowFetcher
instance is used only for 1 userId
– it might me a good idea to add __init__
method:
__init__(self, userId):
# some userId validation
self.userId = userId
and save yourself a bit of passing userId
around.
UPD:
If all options are set to True
, this will call getUserInfo
and, therefore, query api 3 times:
if ( show_display_name ) : print fetcher.getUserDisplayName( userId )
if ( show_view_count) : print fetcher.getUserViewCount( userId )
if ( show_reputation ) : print fetcher.getUserReputation( userId )
Since you call it at least once any way, you better call it in the __init__()
, or just store retrieved value in an instance attribute and use it like this:
def __init__(self, userId):
#...
self.userInfo = None
def getUserReputation(self):
if self.userInfo is None:
self.userInfo = self.getUserInfo()
return self.userInfo['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/string_id returns
{
"error": {
"code": 404,
"message": "The server has not found anything matching the Request-URI."
}
}
and will raise KeyError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/9924 returns
{
"total": 0,
"page": 1,
"pagesize": 30,
"users": []
}
and will raise IndexError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
And since you are using userId
as an argument to every method, and the StackOverflowFetcher
instance is used only for 1 userId
– it might me a good idea to add __init__
method:
__init__(self, userId):
# some userId validation
self.userId = userId
and save yourself a bit of passing userId
around.
http://api.stackoverflow.com/1.1/users/string_id returns
{
"error": {
"code": 404,
"message": "The server has not found anything matching the Request-URI."
}
}
and will raise KeyError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/9924 returns
{
"total": 0,
"page": 1,
"pagesize": 30,
"users": []
}
and will raise IndexError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
And since you are using userId
as an argument to every method, and the StackOverflowFetcher
instance is used only for 1 userId
– it might me a good idea to add __init__
method:
__init__(self, userId):
# some userId validation
self.userId = userId
and save yourself a bit of passing userId
around.
UPD:
If all options are set to True
, this will call getUserInfo
and, therefore, query api 3 times:
if ( show_display_name ) : print fetcher.getUserDisplayName( userId )
if ( show_view_count) : print fetcher.getUserViewCount( userId )
if ( show_reputation ) : print fetcher.getUserReputation( userId )
Since you call it at least once any way, you better call it in the __init__()
, or just store retrieved value in an instance attribute and use it like this:
def __init__(self, userId):
#...
self.userInfo = None
def getUserReputation(self):
if self.userInfo is None:
self.userInfo = self.getUserInfo()
return self.userInfo['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/string_id returns
{
"error": {
"code": 404,
"message": "The server has not found anything matching the Request-URI."
}
}
and will raise KeyError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
http://api.stackoverflow.com/1.1/users/9924 returns
{
"total": 0,
"page": 1,
"pagesize": 30,
"users": []
}
and will raise IndexError
s here:
def getUserDisplayName( self, userId ):
return self.getUserInfo( userId )['users'][0]['display_name']
def getUserViewCount( self, userId ):
return self.getUserInfo( userId )['users'][0]['view_count']
def getUserReputation( self, userId ):
return self.getUserInfo( userId )['users'][0]['reputation']
And since you are using userId
as an argument to every method, and the StackOverflowFetcher
instance is used only for 1 userId
– it might me a good idea to add __init__
method:
__init__(self, userId):
# some userId validation
self.userId = userId
and save yourself a bit of passing userId
around.