Question

PYTHON

4. While Loops (25%) You will be writing a shell application, it will be on a loop, taking in commands from the user. The command syntax is as follows command> <arga» <arg1> arg n> The commands you are to implement: Command Description Prints all arguments on the same line, with a space between each. Treats all arguments as floating-point numbers, and prints the average. MUST print an error if given invalid arguments Finds the index of arg1 in arg0. If there are not at least 2 args, print out an error Reads a text file containing 0 or more of commands, treating it exactly as if it was coming in from standard input. echo average strstr script Example run: (User input is prefixed by [IN]) (Output is prefixed by [OUT]) [IN] echo Hello, World! [OUT] Hello, borld! [IN] average ape [OUT] ERROR: usage: average <number <number> [IN] average 2 1 336 OUT] 3 [IN] strstr a [OUT] ERROR : usage : strstrarga» ?arg1> [IN] strstr pinecone cone OUT1 4 [IN] script introduction.script <number> And, if we have a file, introduction.script containing the following introduction.script echo This is my introduction!!! echo How cool is this? And our application output... [OUT] This is my introduction!!! [OUT] how cool is this?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

**********************************************Code************************************

class ShellApplication:
    def __init__(self):
        # From here we are taking the data from user and based on the command given we are calling functions
        while True:
            print "----------------------------------"
            print "1-Command\n2-stop"
            ch = input()
            if ch == 1:
                print "Enter command:"
                command = raw_input().split(" ")
                print "*****************************"
                if command[0] == "echo":
                    # If first word is echo then we are calling display_text function
                    self.display_text(command)
                elif command[0] == "average":
                    # If first word is average then we are calling print_average function
                    self.print_average(command)
                elif command[0] == "strstr":
                    # If first word is strstr then we are calling find_index
                    self.find_index(command)
                elif command[0] == "script":
                    # If first word is  script then we are calling read_from_file function
                    self.read_from_file(command)
                else:
                    print "Invalid Command"
            elif ch == 2:
                break
            else:
                print "Choose correctly"

    @staticmethod
    def display_text(commands):
        # Here we are joining the text given and printing it
        text = ' '.join(commands[1:])
        print text

    @staticmethod
    def print_average(commands):
        # Here we are converting the given string to float if any error occurs displaying the text else printing avg
        try:
            count = 0
            sum = 0
            for num in commands[1:]:
                sum += float(num)
                count += 1
            print sum / count
        except:
            print "ERROR: usage: average <number> <number> .. <number>"

    @staticmethod
    def find_index(commands):
        # This function will find str2 in str1
        if len(commands[1:]) == 2:
            # If length is 2 then we will find the str2 in str1 else we will show the error message
            find_str = commands[2].replace("\n", "")
            # First here we are checking whether str2 is in str1 or not if it is there then we are printing index
            if find_str in commands[1]:
                print commands[1].index(find_str)
            else:
                print "String is not found!!"
        else:
            print "ERROR: usage: strstr <arg0> <arg1>"

    def read_from_file(self, commands):
        # Here we are reading the text from file line by line and we are processing that based on the given text
        file_name = commands[1].replace("\n", "")
        with open(file_name) as fd:
            content = fd.readlines()
            for line in content:
                split_line = line.split(' ')
                if split_line[0] == "echo":
                    # If first word is echo then we are calling display_text function
                    self.display_text(split_line)
                elif split_line[0] == "average":
                    # If first word is average then we are calling print_average function
                    self.print_average(split_line)
                elif split_line[0] == "strstr":
                    # If first word is strstr then we are calling find_index
                    self.find_index(split_line)
                elif split_line[0] == "script":
                    # If first word is  script then we are calling read_from_file function
                    self.read_from_file(split_line)
                else:
                    print "Invalid Command Line Arguments"


obj = ShellApplication()

*************************************Code Screens********************************

class ShellApplication: def init (self): # From here we are taking the data from user and based on the command given we are c@staticmethod def find index(commands) 50 52 53 54 # This function will find str2 in str1 if len (commands [1:])2: # If lengt

*************************************Output Screens********************************

sys1091@sys1091:~/PycharmProjects/inboxer/tests python test.py 1-Command 2-stop Enter command: echo hello world hello world 11-Command 2-stop Enter command: strstr a RKK ERROR: usage: strstr <arg0> <argl> 1-Command 2-stop Enter command: strstr a b csys1091@sys1091:/PycharmProjects/inboxer/tests cat 1.txt echo This is my introduction!! average 2 1 3 3 6 average hi 2 strstr

Add a comment
Know the answer?
Add Answer to:
PYTHON 4. While Loops (25%) You will be writing a "shell" application, it will be on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”....

    Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”. The script reports and prints (to stdout) selected storage management information. Think of it as “RePort Storage Management”. The easy way to describe what the script needs to do is to look at what it should display in the case where you provide incorrect parameters, or in the case where you provide “-h” for help: Usage: ./rpsm.sh <options>< directory> Reports selected information about specified directory...

  • Overview Writing in the C language, implement a very basic shell, called smash. In this project,...

    Overview Writing in the C language, implement a very basic shell, called smash. In this project, we will work on processing strings and using the appropriate system calls. Build System setup Before we can start writing code we need to get our build system all setup. This will involve writing a very simple makefile. You should leverage your Makefile project for this part of the assignment! Write a Makefile that provides the three targets listed below all - The default...

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • You will write a C program, q1 sequence.c, that computes the value of the nth term...

    You will write a C program, q1 sequence.c, that computes the value of the nth term in any recursive sequence with the following structure: an = c1 · an−1 + c2 · an−2 a0 > 0 a1 > 0 c1 6= 0 c2 6= 0 Your C program will take 5 integer arguments on the command line: n, a0, a1, c1 and c2. n must be an integer greater than or equal to 0. If more or fewer arguments are...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will...

    Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will write a client and server program that enables the client to determine the round-trip time (RTT) to the server. To determine the RTT delay, the client records the time on sending a ping request to the server, and then records the time on receiving a ping response from the server. The difference in the two times is the RTT. The ping message contains 2...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • 1 Overview and Background Many of the assignments in this course will introduce you to topics in ...

    1 Overview and Background Many of the assignments in this course will introduce you to topics in computational biology. You do not need to know anything about biology to do these assignments other than what is contained in the description itself. The objective of each assignment is for you to acquire certain particular skills or knowledge, and the choice of topic is independent of that objective. Sometimes the topics will be related to computational problems in biology, chemistry, or physics,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT