resham Myshellv1 20130719-025856    Posted:


Problem

1.To get "hi username" as output on giving greet command.

2.To get stock value of GOOG on giving stockGOOG command.

Solution

from cmd2 import Cmd

from getpass import getuser

import requests

import sys

__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_greet(self,line):
print "hi,%s" % (getuser())
def do_stockgoog(self,line):

link = requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=l1")

stockvalue=r.text

print stockvalue

if __name__ == '__main__':
app = Application() app.cmdloop()

Commands

$python psh.py

Comments

Devyani-Divs myshellv1 20130718-204314    Posted:


Myshellv1

Problem:

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.

Program-Code

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()

Explanation of the code

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>

Comments

piotrektt mount homework 20130718-203004    Posted:


Problem

To write a program which behaves as mount command

Solution

execute
$ python mounts.py

Comments

anurag619 myshellv1 20130718-200825    Posted:


myshellv1

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.

solution to the problem

#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()

Comments

piotrektt Introduction 20130718-195648    Posted:


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

Comments

Christina-B planetfeedparser 20130718-191714    Posted:


Problem- Generate a program in python to print the name of author and the title present on blog using feedparser module.

Description

  1. Install feedparser in the virtualenv.

  2. Parse the url on which the blog is present.

  3. Calculate the total number of entries present after parsing.

  4. For each entry print the title and name of the author.

    Click the link below to see the program

    link

    Execute the program using following command

    $ chmod +x feedparse.py

    $ python feedparse.py

Comments

iowabeakster myshell 20130718-191254    Posted:


basic functions of script

  • import necessary modules
  • take 4 prompt inputs and give the requested outputs

script code

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()

Comments

rahulc93 myshellv1 20130718-175959    Posted:


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.

Source

The code for the problem can be found here

Solution

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()

Run the program

To run the program, save it as psh.py, and:

$ python psh.py

Comments

puspita23 Myshellv2 20130718-171531    Posted:


Problem

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>"

Solution

From 'Greet' command I have used getuser() From 'Stock <nasdaqsymb> I have used requests.get()

Code

 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

Comments

Christina-B myshellv1 20130718-165300    Posted:


Problem- Generate a program in python which will generate two commands using the cmd package.

Description

  1. Import cmd, os, urllib2

  2. class myshell defines two functions do_greet(), do_stock()

  3. do_greet function will take the user name from the system and reflect the result as hi username when the command hi is executed.

  4. 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

    link

    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

Comments

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