JCaselles myshellv1 20130718-162028


Posted:

My Shell v1

Assignment: Wrote a script that will use cmd2 module to emulate a command shell. We need to define 2 functions for this shell: greet, which shall return "Hi, <user>" and stock <NASDAQSYMB>, which shall return the current share value of the given company.

Solution:

For the user greeting function, i've used getpass.getuser(). For the share value, requests module has been used, along with yahoo finance service: download.finance.yahoo.com

Code:

Link to the Code

 1 #!/usr/bin/env python
 2 
 3 import requests
 4 from cmd2 import Cmd
 5 from sys import exit
 6 from getpass import getuser
 7 
 8 
 9 class Application(Cmd):
10     """
11     Main Application class
12 
13     """
14 
15     def __init__(self):
16         Cmd.__init__(self)
17 
18 
19     def do_greet(self, line):
20         """
21         Prints a greeting for the current user.
22 
23         """
24 
25         print "Hi, %s" % getuser()
26 
27 
28     def do_stock(self, line):
29         """
30         This method prints the share value of the company whose nasdaq symbol
31         is given. It is argument safe: no matter how many arguments you pass
32         to the command, it will only use the first one.
33 
34         returns the share value if the symbol is correct.
35         Otherwise, returns error.
36 
37         """
38 
39 
40         content = requests.get("http://download.finance.yahoo.com"
41                                 "/d/quotes.csv?s=%s&f=l1" % line.split(" ")[0])
42 
43         if content.text.find("0.00") == -1:
44             print content.text.rstrip()
45 
46         else:
47             print "Error. Make sure the symbol is a valid NASDAQ symbol"
48 
49 
50 
51 if __name__ == "__main__":
52     app = Application()
53     app.cmdloop()
Contents © 2013 dgplug - Powered by Nikola
Share
UA-42392315-1