.. link: http://dgplug.org/summertraining/2013/posts/puspita23-myshellv1-20130718-164810.html .. description: .. tags: .. date: 2013/07/18 16:48:11 .. title: puspita23 Myshellv1 20130718-164810 .. slug: puspita23-myshellv1-20130718-164810 Problem ------- Write a script to define 2 commands 'stock' and 'greet' for the shell cmd2 imported from Cmd module. 1.Stock will return the current sharevalue of the symbol entered. 2.Greet, will return "Hi!, " Solution -------- From 'Greet' command I have used getuser() From 'Stock I have used requests.get() Code ---- :: from cmd2 import Cmd from getpass import getuser import sys import requests __version__ = '0.1' class Application(Cmd): """ The main Application class """ def __init__(self): Cmd.__init__(self) def do_hello(self, line): print "Hello:", line def do_sayit(self,line): print "Python Rocks!" def do_stock(self,line): """ using requests.get() to get the url and then printing the value of the stock inputted """ r=requests.get('http://download.finance.yahoo.com/d/quote.csv?s='+line+'&f=l1') data=r.text value=float(data) print value def do_greet(self,line): """ getuser() used to get the user that is logged in """ print "Hi! %s" %(getuser()) if __name__ == '__main__': app = Application() app.cmdloop() Link to the code is https://github.com/puspita23/puspita_utility/blob/master/myshell/psh.py