m0rin09ma3 Sharevalue 20130712-093126    Posted:


This program will print the last traded price of the company whose symbol is given to the program as command-line arguments like:

$ python sharevalue.py GOOG YHOO ...

A link to the source code.

Sample outputs:

$ python sharevalue.py GOOG YHOO
GOOG    YHOO
920.24  27.04

Explanation

In the main function, take command-line arguments and construct url string.

n_argc = len(sys.argv)
if n_argc < 2:
    print 'please specify symbol and try again.'
    print 'e.g. sharevalue.py GOOG YHOO ...'
    return 1
#print n_argc

l_symbol = []
l_symbol = [ sys.argv[n] for n in range(1, n_argc) ]
#print l_symbol

# s_symbol = 's=GOOG&s=YHOO&...'
s_symbol = ""
s_symbol = '&'.join( [ 's=' + l_symbol[n] for n in range(n_argc - 1) ] )
#print s_symbol

s_url = ""
s_url = 'http://download.finance.yahoo.com/d/quotes.csv?' + \
s_symbol + \
'&f=l1'
#print s_url

Retrieve data from the URL and store them into a list.

try:
    f = urllib2.urlopen(s_url)
except urllib2.URLError:
    print 'failed to open url', s_url
else:
    l_quote = []
    l_quote = f.readlines() # e.g. ['920.24\r\n', '27.04\r\n']
    f.close()
#print l_quote

Format strings and output.

print "%s".ljust(6) % '\t'.join(l_symbol)
l_quote = [ s.strip() for s in l_quote ]
print "%6s" % '\t'.join(l_quote)

Comments

PrithaGanguly Sharevalue 20130712-065205    Posted:


Problem

Write a program to print the latest share value of a company whose NASDAQ symbol would be provided in the command line.

Code Snippet

#!/usr/bin/env python
import urllib2
import sys

# a function share_price is defined to get the latest share value of a company
def share_price(nasdaqsym):
#the link to the finance site is made abd the data is stored in the instance "share_value"
    share_value = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+nasdaqsym+'&f=l1')

   #The share price is printed
    print "Latest share price of company with NASDAQ symbol "+nasdaqsym+" : " +share_value.read()

if __name__ == '__main__':
#Checking included whether we have one Symbol in the command line or not as per the syntax "./sharevalue    .py [NASDAQ symbol]
     if len(sys.argv) == 2:
         share_price(sys.argv[1])
     else:
         print "Give only one NASDAQ symbol at a time after command line argument!"
 sys.exit(0)

Explanation

For the above stated problem, to obtain the latest share value of a company using the company's NASDAQ symbol, the function urlopen() of the module urllib2 has been used. It takes the link of the website finance.yahoo.com as input and returns the latest share value(specified by the sybmol f=l1 in the link) of the company which is stored in the instance share_value. Then in '__main__', a checking is included which checks whether there is only one input symbol given after the command line input i.e ./sharevalue.py.

Comments

iowabeakster sharevalue 20130712-005534    Posted:


basic functions of script

  • import necessary modules
  • take stock ticker symbol as a command line argument
  • make several evaluative conditions to ensure proper arguments are used
  • attempt to retrieve quotes
  • give another waning if quote returned has no value (likely not a valid symbol)
  • display quote

script code

#!/usr/bin/env python


import urllib2
import sys


# a series of command line checks for sanity
# check to make sure an arg is given for the ticker symbol
if len(sys.argv) < 2:
    print "Come on dude, I need a ticker symbol to look up"
    print "For example,  $ ./sharevalue GOOG"
    print "Try it again."
    sys.exit(1)

# check to make sure only one symbol is given
elif len(sys.argv) > 2:
    print "Please just give me one ticker symbol to look up"
    print "For example,  $ ./sharevalue GOOG"
    print "Try it again."
    sys.exit(1)

# set ticker symbol argv[1] to variable s for easier usage
s = sys.argv[1]

# check to see if arg is alphanumeric
if s.isalnum() == False:
    print "Oh come on man! this isn't that hard"
    print "For example,  $ ./sharevalue GOOG"
    print "It must be alphanumeric, try again."
    sys.exit(1)

# check to see if arg is less that 5 characters
elif len(s) > 4:
    print "Don't you know?"
    print "A ticker symbol is not more than 4 characters in length."
    print "For example,  $ ./sharevalue GOOG"
    sys.exit(1)

# pass the ticker symbol to the url string
ticker_name = ('http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1'
    % sys.argv[1])

# go get the stock quote and cast the returned data to a string and float
get_quote = urllib2.urlopen(ticker_name)
q = get_quote.read()
float(q)

# determine if it was an actual quote
if float(q) < 0.001:
    print "Whoa, either that is a real dog of stock,"
    print "or you still need to enter a vaild ticker symbol."
    print "To see the quote for Google try $ ./sharevalue goog"
    sys.exit(1)

# display the quote
else:
    print q

Comments

iowabeakster mount....... 20130712-001044    Posted:


My solution to the "mount" home task is very simple.

  • import the module called 'os'
  • pass the argument "mount" to the function called 'system.os'.

The ouput is the same as the mount command, because it does exactly the same thing, but does it from a python script.

The link to my code can be found here .

Comments

iamsudip sharevalue 20130711-191249    Posted:


This program will print the current share price of the company whose NASDAQ value is given to the program as an argument like:

./sharevalue.py [NASDAQ symbol]

Code

#!/usr/bin/env python

import urllib2
import sys

def main(nasdaq):
    """
    Here all work is being done.

    :arg nasdaq: NASDAQ symbol
    """

    #making the link in 'link' string to fetch data'
    link = 'http://download.finance.yahoo.com/d/quotes.csv?s='+nasdaq+'&f=l1'
    print 'Fetching data from: '+link

    #data saved into an instance share_value
    share_value = urllib2.urlopen(link)

    #Printing share_value
    print 'Current share price of company '+nasdaq+': '+share_value.read()

if __name__=='__main__':
    if len(sys.argv) == 2:
        main(sys.argv[1])
    else:
        print "Syntax: ./sharevalue.py [NASDAQ Symbol]"
    sys.exit(0)

How to execute code

Run the above script like:

$ ./sharevalue.py [NASDAQ value]

or:

$ python sharevalue.py [NASDAQ value]

Example output

Here some example output is given below:

$ ./sharevalue.py ORCL

Fetching data from: http://download.finance.yahoo.com/d/quotes.csv?s=ORCL&f=l1

Current share price of company ORCL: 31.9449

$ ./sharevalue.py GOOG

Fetching data from: http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=l1

Current share price of company GOOG: 919.27

$./sharevalue.py GOOG ORCL

Syntax: ./sharevalue.py [NASDAQ Symbol]

Comments

elitalobo Mount 20130711-145402    Posted:


Mount

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.

Code Description

Basically we first open and read the mounts file in the proc directory which inturn is present in the root directory.This is done using s=open("/proc/mounts") command followed by f=s.read() . By default the file content is not the same as output obtained by giving mount command on the CLI. So we need to assign the file contents to a variable and then manupulate it using basic python commands so as to achieve the desired output.

The contents of the file which is read by a variable is first split into individual lines using f.split("n"). Each line is split into list of words using z[i].split(" "). Each list of words is then manupulated using insert() method , to insert words in the list, replace() method, to replace words or parts of words in the list, del() method, to delete words from the list and append(), method to add words to the end of the list. Lastly we use join() method to join all the words in the list to form lines which is then printed as output. The /proc/mounts file is closed at the end.

link to the code ` link <https://github.com/elitalobo/HomeTask1/tree/master/mount>`_

Comments

Bidisha2git mount 20130711-144047    Posted:


Solution


The code is all about printing the contents of the mount command.

At 1st we open the file /proc/mounts and then make changes so that the output comes out to be the contents of the mount command. Then we read the file line by line by the function readline(). then looping through each line we insert and delete certain elements as is given in the code below.

Code


#!/usr/bin/env python

f = open("/proc/mounts")#opens the file /proc/mounts

l = f.readlines()# reads the file line by line

for i in l[1:]:# for loop which ignores the first line of the file

a = i.split(" ")#splits the file line by line according to spaces

del a[-2]#deletes the second last zero from the file

del a[-1]#deletes the last zero from the file

a.insert(2,"type ")# the string "type" is inserted in index 2

a.insert(1,"on")# the string "on" is inserted in index 1

a.insert(5,"(")# the opening bracket "(" is inserted in the 5th index

a.insert(7,")")# the closing bracket ")" is inserted in the 7th index

str = " ".join(a)# the splitted string is joined using the join() functi on

print str #the required output is printed; for loop ends

f.close()# file is closed

Link


The link to the code is:

https://github.com/Bidisha2git/Training_tasks_dgplug/blob/master/mount.py

Comments

Devyani-Divs mount 20130711-141753    Posted:


Mount Assignment

Code in python to print the exact output as received on carrying out the 'mount' command.

Explanation of the code

1.)In the program, we are initially using the open() function, to open the "/proc/mounts" file. 2.)And then, we are using a for loop to split the lines of the file one by one and then carry out the required manipulations in the file in order to get the exact output as one would have received on carrying out the mount command. 3.)Further, join() function is used to join the split strings and finally, printing the required output as desired. 5.)Lastly, we use the close() function to close the edited file.

Link to the actual code file is: '<https://github.com/Devyani-Divs/Home-tasks_dgplug/blob/master/mount.py>'

Comments

resham mount 20130711-141446    Posted:


Solution

The code written below gives the same output as that given by the mount command on the terminal. The code first opens a file named mounts present in /proc directory,then reads its contents line by line and then prints the contents(required output)after carrying functions such as strip,insert, delete etc.then we closes this file to prevent the program from crashing.

Code

1.#!/usr/bin/env python 2.f = open("/proc/mounts") #this opens the mounts file present inside p roc directory 3. b= f.readlines() #reads the text inside the mounts file.

3.for x in b[1:]: 4. l = x.split(" ") 5. del l[-2] 6. del l[-1] 7. l.insert(2,"type ") 8. l.insert(1,"on") 9. l.insert(5,"(") 10. l.insert(7,")") 11. str = " ".join(l) 12.print str 13.f.close() #closes the file after use.

Comments

Christina-B mount v2 20130711-140117    Posted:


Problem- Generate a program in python which will produce the same output as the mount command.

Description

  1. The file is opened in read mode using f = open("/proc/mounts"). The default mode is read.

  2. Each line of the file is read For each line a function call is made to function func The function func is responsible for converting the string which is passed to it into a list. The last two elements of the list are deleted. Then on,type,( and ) are added to the list. The list is then converted into string and printed on the console.

  3. After the operation is complete file is closed.

    Click the link below to see the program

    link

    Execute the program using following command

    $ chmod +x mount.py

    $ ./mount.py

Comments

Contents © 2013 dgplug - Powered by Nikola
Share
UA-42392315-1