.. link: http://dgplug.org/summertraining/2013/posts/tenstormavi-sharevalue-20130715-134052.html .. description: .. tags: .. date: 2013/07/15 13:40:53 .. title: tenstormavi Sharevalue 20130715-134052 .. slug: tenstormavi-sharevalue-20130715-134052 Sharevalue ========== Our assignment is to print the latest share value of a company whose NASDAQ symbol would be given as command line argument. Code ==== :: #!/usr/bin/env python import urllib2 #<---------------------------------- the module is imported for opening url import sys #<-----------------------------------sys module is imported for command line arguments def share(symbol): #<-------------The function is defined for opening and reading the url, and for printing sharevalue try: #---------------Used for handle exception and error may be due to problems like url not opening link = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+symbol+'&f=l1') #<------opens the url r = float(link.read()) #<----------------------------------Reads the url if r == 0.00: #<---------------------if any wrong symbol is entered then it print the share print "The Nasdaq code entered is wrong" else: print "The current sharevalue for the given NASDAQ symbol is %f" % (r) except: print "failed to open the finance.yahoo.com url" if __name__ == '__main__': if len(sys.argv) !=2: print "Enter a valid NASDAQ symbol" sys.exit(1) else: share(sys.argv[1]) sys.exit(1) Link ==== Code is here: `sharevalue.py`_ .. _sharevalue.py: https://github.com/tenstormavi/dgplug_home_tasks/blob/master/sharevalue/sharevalue.py Run === How to run the above command:: $python sharevalue.py