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 ( show_display_name ) : print fetcher.getUserDisplayName( userId )
if ( show_view_count) : print fetcher.getUserViewCount( userId )
if ( show_reputation ) : print fetcher.getUserReputation( userId )