the program is based on printing the current shrevalue of the nsdaq
#!/usr/bin/env python import urllib2 import sys def get_value(nasdaq): #to get the share value of nasdaq this function will get the share value of nasdaq url='http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % (nasdaq) #this the url of the nasdaq syambol extracted from yahho.finance con=urllib2.urlopen(url) #to open the url val=con.read() #to read the current share value of nasdaq symbol from yahoo.finanace print "the current sharevalue is %s" % (val) if __name__ == '__main__': #it calls the function to get the current sharevalu if len(sys.argv) == 2: #condition to check whter the file name in the command line does not exceed two it will check for the number of arguments in command line with ./shareval.py get_value(sys.argv[1]) #passe the nasdaq to the main user else: print "Give only one NASDAQ symbol at a time after command line argument!" sys.exit(0)
Here we use import urllib2 to get the current share value of symbol NASDAQ by using a function get_val.Now we open the url lin and read it to get the current sharevalue and print it. '_main_' is used to check weather in the command line input only one input symbol is given or more than one.If it exceed two includin the ./shareval.py then it will exit else it will print the current sharevalue of the symbol
the link to the code is https://github.com/supriyasaha/hometaskrepo/blob/master/sharevalue/shareval.py
Task Write a file mount.py which when executed as ./mount.py gives the same output as mount command. For this we require to read /proc/mounts.
readfile Function
changefile Function
link to the code ` link <https://github.com/elitalobo/HomeTask1/tree/master/mount>`_
the program is based on rinting the current shrevalue of the nsdaq
#!/usr/bin/env python
import urllib2 import sys
def get_value(nasdaq): #to get the share value of nasdaq
this function will get the share value of nasdaq
url='http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % (nasdaq) #this the url of the nasdaq syambol extracted from yahho.finance con=urllib2.urlopen(url) #to open the url val=con.read() #to read the current share value of nasdaq symbol from yahho.finanace print "the cureent sharevalue is %s" % (val)
if __name__ == '__main__': #it recieves the desire coed and calls tha funtion to geth the current sharevalu
if len(sys.argv) == 2: #condition to check whter the file name in the command line does not exceed two
it will check for the number of arguments in command line with ./shareval.py
get_value(sys.argv[1]) #passe the nasdaq to the main user
else:
print "Give only one NASDAQ symbol at a time after command line argument!"
sys.exit(0)
Here we use import urllib2 to get the current share value of symbol NASDAQ by using a function get_val.Now we open the url lin and read it to get the current sharevalue and print it. '_main_' is used to check weather in the command line input only one input symbol is given or more than one.If it exceed two includin the ./shareval.py then it will exit else it will print the current sharevalue of the symbol
the link to the code is https://github.com/supriyasaha/hometaskrepo/blob/master/sharevalue/shareval.py
Hi, my name is Rahul Mishra, I am pursuing my B-TECH from NSEC(kolkata) with CSE stream, currently i am in final year.
This training is very useful and unique in the form it is conducted, looking forward to it. :)
sharevalue
To print the latest price of the company whose NASDAQ symbol will be given in the command line
The code is given here :- link
We can run the program like :-
$ chmod +x sharevalue.py
$ python sharevalue.py <NASDAQ symbol>
step 1: From the main function check whether the input provided is a valid input or not. step 2: If its a valid input, pass the symbol as an argument in the function, else it will display an error message and exit. step 3: This function will provide the quoted price of the share and will print it.
quote= urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+sym+'&f=l1') #quote is an instance variable which is storing the quoted price at that instance, of that NASDAQ symbol
#Here is the main function where the argument passed in the commandline is checked to see whether it is a valid input or not.
link to the code: https://github.com/puspita23/puspita_utility/commit/291123932a2db6aca2fad1dac2133cc21118cb0c
The script checks if the command entered is in the correct format. If true, then getvalue() function is called and the NASDAQ symbol is passed to it. The getvalue() function fetches the share value from the url using urlopen() function of module urllib2 and stores it in a file. It the passes the file to printvalue() function which prints the share value.
#!/usr/bin/env python import sys import urllib2 def getvalue(symbol): """ This function gets the latest share value of the inputted NASDAQ symbol. :arg symbol: NASDAQ symbol of a company. """ url = 'http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % symbol # url for getting share value value = urllib2.urlopen(url) # file containing share value printvalue(value) # file passed to printvalue() which prints its contents def printvalue(val): """ This function prints the latest share value of the inputted NASDAQ symbol :arg val: Share value of imputted NASDAQ symbol of a company. """ print "Share value: %s" % val.read() if __name__ == '__main__': if len(sys.argv) != 2: #checks if command line parameters are equal to 2 or not print "Wrong parameter" print "Enter the command in this format: ./sharevalue.py <a valid NASDAQ symbol>" sys.exit(1) getvalue(sys.argv[1]) #NASDAQ symbol passed to getvalue() if entered command is in correct format
The script can be found here
here i am python script which will show us the latest nasdaq value. we will be using urllib2 module to fetch the url.
sharevalue.py code link.
import urllib2 #imports module urllib2. import sys #imports module sys. def nasdaq(value): #creating a function nasdaq. find = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1'%value) #urlopen function of urllib2 fetches the url along with the argument. print "NASDAQ value of %s is %s " %(value.upper(), find.read()) #prints the nasdaq value. find.close() def main(): #function main. if len (sys.argv)!= 2: #checking the length of arguments(condition). print "enter the script and the NASDAQ sysmbol, like GOOG,FB,YHOO etc" #print the condition statement. else: nasdaq(sys.argv[1]) #nasdaq function called. sys.exit(0) if __name__ == '__main__': #standard condition to call the main function. main()
to run the code
$python sharevalue.py FB
In this problem we will take a NASDAQ code as input from the user and return the current share value for that company
The code to the problem can be found here
Here is the solution to the the problem.
#!/usr/bin/env python import urllib2 import sys def get_value(nasdaq): """ This function fetches the current share value according to the NASDAQ code given by the user and displays it. :arg nasdaq: NASDAQ code given by the user """ link = 'http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % (nasdaq) # store the recquired URL acording to the NASDAQ value in the 'link' variable link_open = urllib2.urlopen(link) # opening the URL price = float(link_open.read()) # store the value retrieved from the URL in the 'price' variable if price == 0.00: # incorrect NASDAQ value given by user print 'The NADAQ code entered is wrong. ' # display an error message else: print 'The latest share value is %s' % (price) # display the value obtained from the URL def main(nasdaq): """ This function recieves the NASDAQ code and calls the desired function to fetch the current share value. :arg nasdaq: NASDAQ code given by the user """ get_value(sys.argv[1]) # calls the 'get_value' method, passing to it the first command line parameter as argument if __name__ == '__main__': if len(sys.argv) != 2: # incorrect syntax for executing the program from command line print 'Wrong syntax. ' # display error message sys.exit(-1) # abnormal termination else: #syntax is ok main(sys.argv[1]) # pass the NASDAQ code to main given by user sys.exit(0) # successful termination
To run this script, follow the steps.
Change the file's permissions and make it executable:
$ chmod +x sharevalue.py
Execute the file:
$ ./sharevalue.py
Alternatively, you can also do:
$ python sharevalue.py
In this problem we will take a NASDAQ code as input from the user and return the current share value for that company
The code to the problem can be found here
Here is the solution to the the problem.
#!/usr/bin/env python import urllib2 import sys def get_value(nasdaq): """ This function fetches the current share value according to the NASDAQ code given by the user and displays it. :arg nasdaq: NASDAQ code given by the user """ link = 'http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % (nasdaq) # store the recquired URL acording to the NASDAQ value in the 'link' variable link_open = urllib2.urlopen(link) # opening the URL price = float(link_open.read()) # store the value retrieved from the URL in the 'price' variable if price == 0.00: # incorrect NASDAQ value given by user print 'The NADAQ code entered is wrong. ' # display an error message else: print 'The latest share value is %s' % (price) # display the value obtained from the URL def main(nasdaq): """ This function recieves the NASDAQ code and calls the desired function to fetch the current share value. :arg nasdaq: NASDAQ code given by the user """ get_value(sys.argv[1]) # calls the 'get_value' method, passing to it the first command line parameter as argument if __name__ == '__main__': if len(sys.argv) != 2: # incorrect syntax for executing the program from command line print 'Wrong syntax. ' # display error message sys.exit(-1) # abnormal termination else: #syntax is ok main(sys.argv[1]) # pass the NASDAQ code to main given by user sys.exit(0) # successful termination
To run this script, follow the steps.
Change the file's permissions and make it executable:
$ chmod +x sharevalue.py
Execute the file:
$ ./sharevalue.py
Alternatively, you can also do:
$ python mount.py