Write a program that constitutes the following two commands:
1)greet : which will say "Hi" to the user.
2)stock : which will print the cureent stock value.
#The above commands are to be executed using the requests library.
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_greet(self,line): print "Hi %s :)" %(getuser()) def do_stock(self,line): r=requests.get('http://download.finance.yahoo.com/d/quote.csv?s='+line+'&f=l1') data=r.text print data if __name__ == '__main__': app = Application() app.cmdloop()
In the code, the greet function uses the getuser() in order to get the name of the user, and displays "Hi <username>" as output.
And the stock function uses the requests.get() function to get the latest value of the stock-value, and further prints the value on taking the nasdaq symbol as input.
The link for the code is : <https://github.com/Devyani-Divs/Home-tasks_dgplug/blob/master/myshell/myshellv1.py>