1.To get "hi username" as output on giving greet command.
2.To get stock value of GOOG on giving stockGOOG command.
from cmd2 import Cmd
from getpass import getuser
import requests
import sys
__version__ = '0.1'
""" The main Application class
"""
link = requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=l1")
stockvalue=r.text
print stockvalue
$python psh.py
The link of actual file is https://github.com/resham/summer-training-hometask/blob/master/psh.py
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>
To write a program which behaves as mount command
_link: https://github.com/piotrektt/dgplug/tree/master/mounts/mounts.py
the following script will give an output when the command "greet" and "stock".
module getpass is used to print the user name.
the code for sharevalue can be found here -full sharevalue code, it has been modified here.
myshellv1.py code link
#python script to get a shell with (Cmd) as prompt from cmd2 import Cmd import getpass import requests __version__ = '0.1' class Application(Cmd): """ The main Application class """ def __init__(self): #inherits Cmd and any method starting with do_ becomes a command Cmd.__init__(self) def do_hello(self, line): print "Hello:", line def do_sayit(self, line): #command sayit which will print "python rocks" print "Python Rocks!" def do_greet(self,line): #greet command print "Hi, %s" %(getpass.getuser()) def do_stock(self,line): #stock command will print the latest NASDAQ value when given in a valid value url = requests.get('http://download.finance.yahoo.com/d/quotes.csv?s='+line+'&f=l1') stock_value = (url.text) print stock_value if __name__ == '__main__': app = Application() app.cmdloop()
Hi! My name is Peter. Currently I have two lives - one as an English teacher in a Primary School somwhere in the middle of nowhere in Poland :), the second life is with computers and Linux.
I am gaining experience as a linux administrator, but my primary task is to set up and work with ha/failover storage and cluster solutions (glusterfs, zfs, drbd etc.)
I want to learn python, because it rocks! :P
Problem- Generate a program in python to print the name of author and the title present on blog using feedparser module.
Description
Install feedparser in the virtualenv.
Parse the url on which the blog is present.
Calculate the total number of entries present after parsing.
For each entry print the title and name of the author.
Click the link below to see the program
- Execute the program using following command
$ chmod +x feedparse.py
$ python feedparse.py
note: this script was meant to be ran in a virutalenv with needed modules installed, as in session work
from cmd2 import Cmd import os import pwd import requests __version__ = '0.1' # function to provide the current username def get_username(): return pwd.getpwuid( os.getuid() )[ 0 ] class Application(Cmd): """ The main Application class """ def __init__(self): Cmd.__init__(self) # provided hello input function def do_hello(self, line): print "Hello:", line # sayit function prints indicated text, conveying that python does "Rock!" def do_sayit(self, line): print "Python Rocks!", line # home task assignment to repond to the input "greet" with "Hi, <username>" def do_greet(self, line): user = get_username() print "Hi,", user # home task to provide stock quote using the requests lib def do_stock(self, line): ticker = line r = requests.get( 'http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % ticker ) print r.text if __name__ == '__main__': app = Application() app.cmdloop()
In this assignment. we will create a virtualenv and run this script which will create a small shell like environment with 'Cmd' as prompt, and where we can give some commands to it.
The code for the problem can be found here
The solution to the above problem is given below.
from cmd2 import Cmd import requests import pwd __version__ = '0.1' class Application(Cmd): """ The main Application class """ def __init__(self): Cmd.__init__(self) def do_hello(self, line): # defining the 'hello' command print "Hello:", line # executing the instructions for the command def do_sayit(self, line): # defining the 'sayit' command print "Python rocks!" # executing the instructions for the command def do_stock(self, line): # defining the 'stock' command get_stock() # calling the 'get_stock' method to execute the command def do_greet(self, line): # defining the 'greet' command print_user() # calling the 'print_user' method to execute the command def get_stock(): """ This function uses the requests module and extracts the share price from the recquired URL according to the NASDAQ value given by the user """ nasdaq = raw_input("Enter NASDAQ code: ") # taking the NASDAQ code as input from the user url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1" \ % (nasdaq) # the recquired URL from which the share price shall be obtained value = requests.get(url).text # storing the share price from the URL in 'value' if value == 0: # incorrect NASDAQ code provided print "Incorrect NASDAQ symbol." # generate error message else: # share price has been obtained print "The current share value is ", value # display the value def print_user(): """ This function uses the pwd module and will print the name of the user currently logged in """ total_list = pwd.getpwall() # a list of all password database entries for x in total_list: # iterating through the list if x[5].find('home') == 1: # the entry for the user has been found print "Hello %s" % x[0] # print the user name break # exit from the loop if __name__ == '__main__': app = Application() app.cmdloop()
To run the program, save it as psh.py, and:
$ python psh.py
Write a script to define 2 commands 'stock' and 'greet' for the shell cmd2 imported from Cmd module. 1.Stock <nasdaqsymb> will return the current sharevalue of the symbol entered. 2.Greet, will return "Hi!, <username>"
From 'Greet' command I have used getuser() From 'Stock <nasdaqsymb> I have used requests.get()
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
Problem- Generate a program in python which will generate two commands using the cmd package.
Description
Import cmd, os, urllib2
class myshell defines two functions do_greet(), do_stock()
do_greet function will take the user name from the system and reflect the result as hi username when the command hi is executed.
do_stock will take the symbol of the company and print the stock value for this urlopen() is used.
Click the link below to see the program
- Execute the program using following command
$ chmod +x myshellv1.py
$ python myshellv1.py
OUTPUT
(cmd) greet
Will produce--> Hi christina
(cmd) stock GOOG
Will produce--> Stock value is 914.90