HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Stack Overflow user info fetcher

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
infostackoverflowuserfetcher

Problem

I'm starting to look at Python and its facilities. I've just tried to make one simple program using some base libraries.

I would like to hear some comments about anything you've noticed - style, readability, safety, etc.

```
#!/usr/bin/python2.6 -u

import urllib2
import simplejson
import zlib
from optparse import OptionParser

class StackOverflowFetcher:
"""Simple SO fetcher"""
def getUserInfo( self, userId ):
response = urllib2.urlopen( 'http://api.stackoverflow.com/1.1/users/' + str(userId) )
response = response.read()
jsonData = zlib.decompress( response, 16+zlib.MAX_WBITS )
return simplejson.loads( jsonData )

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']

if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-u", "--userId", dest="userId", help="Set userId (default is 1)", default=1 )
parser.add_option("-n", "--display-name", action="store_true", dest="show_display_name", default=False, help="Show user's display name")
parser.add_option("-v", "--view_count", action="store_true", dest="show_view_count", default=False, help="Show user's profile page view count")
parser.add_option("-r", "--reputation", action="store_true", dest="show_reputation", default=False, help="Show user's reputation")
(options, args) = parser.parse_args()

userId = options.userId
show_display_name = options.show_display_name
show_view_count = options.show_view_count
show_reputation = options.show_reputation

if ( (not show_display_name) and (not show_view_count) and (not show_reputation) ):
show_display_name = show_view_count = show_reputation = True

fetcher = StackOverflowFetcher()
if

Solution

I think your naming scheme is good and on first sight its very easy to see what the code does without having to study it too much.

However, if I would pick on something I would say:

-
Remove the unnecessary whitespace, ex. if ( condition ), or self.getUserInfo( userId ). This is of course a matter of taste, but I find it more consistent with regular coding style to not 'bloat' with whitespace.

-
optparser was deprecated in 2.7: http://docs.python.org/library/optparse.html

-
in parser.add.option() you could remove the word "show" from the dest name and rather do the following, hence eliminating the need for all the declarations of show_display_name and still keep easy code readability.

...
parser.add.option(parser.add_option("-r", "--reputation", action="store_true", dest="reputation", default=False, help="Show user's reputation")
(show, args) = parser.parse_args()

fetch = StackOverflowFetcher()
if(show.reputation) : print fetch.getUserReputation(show.userId)


-
I don't really see what the point of the following line which sets everything to true, it seems a bit weird since you use store_true as action if the option parsed is set.

if ( (not show_display_name) and (not show_view_count) and (not show_reputation) ):
show_display_name = show_view_count = show_reputation = True

Code Snippets

...
parser.add.option(parser.add_option("-r", "--reputation", action="store_true", dest="reputation", default=False, help="Show user's reputation")
(show, args) = parser.parse_args()

fetch = StackOverflowFetcher()
if(show.reputation) : print fetch.getUserReputation(show.userId)
if ( (not show_display_name) and (not show_view_count) and (not show_reputation) ):
show_display_name = show_view_count = show_reputation = True

Context

StackExchange Code Review Q#7930, answer score: 7

Revisions (0)

No revisions yet.