Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me with this program. I very appreciate your help Consider the Python script randline.py. #!/usr/bin/python Output lines selected randomly from

Can someone please help me with this program. I very appreciate your help

Consider the Python script randline.py.

#!/usr/bin/python """ Output lines selected randomly from a file Copyright 2005, 2007 Paul Eggert. Copyright 2010 Darrell Benjamin Carbajal. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Please see for a copy of the license. $Id: randline.py,v 1.4 2010/04/05 20:04:43 eggert Exp $ """ import random, sys from optparse import OptionParser class randline: def __init__(self, filename): f = open(filename, 'r') self.lines = f.readlines() f.close() def chooseline(self): return random.choice(self.lines) def main(): version_msg = "%prog 2.0" usage_msg = """%prog [OPTION]... FILE Output randomly selected lines from FILE.""" parser = OptionParser(version=version_msg, usage=usage_msg) parser.add_option("-n", "--numlines", action="store", dest="numlines", default=1, help="output NUMLINES lines (default 1)") options, args = parser.parse_args(sys.argv[1:]) try: numlines = int(options.numlines) except: parser.error("invalid NUMLINES: {0}". format(options.numlines)) if numlines < 0: parser.error("negative count: {0}". format(numlines)) if len(args) != 1: parser.error("wrong number of operands") input_file = args[0] try: generator = randline(input_file) for index in range(numlines): sys.stdout.write(generator.chooseline()) except IOError as (errno, strerror): parser.error("I/O error({0}): {1}". format(errno, strerror)) if __name__ == "__main__": main()

Write a new script shuf.py in the style of randline.py; your script should implement the GNU shuf command that is part of GNU Coreutils. You should already have a copy of the shuf source code and documentation; look at the files you created in the laboratory. However, GNU shuf is written in C, whereas you want a Python implementation so that you can more easily add new features to it.

Your program should support the following shuf options, with the same behavior as GNU shuf: --input-range (-i), --head-count (-n), --repeat (-r), and --help. As with GNU shuf, if --repeat (-r) is used without --head-count (-n), your program should run forever. Your program should also support zero non-option arguments or a single non-option argument "-" (either of which means read from standard input), or a single non-option argument other than "-" (which specifies the input file name). Your program need not support the other options of GNU shuf. As with GNU shuf, your program should report an error if given invalid arguments.

Your shuf.py program should not import any modules other than argparse, string and the modules that randline.py already imports. Don't forget to change its usage message to accurately describe the modified behavior.

Port your shuf.py implementation to Python 3. If you have trouble with optparse under Python 3, you can use the argparse module instead. Make sure that your shuf.py still works with Python 2. Don't rewrite it from scratch; make as few changes as is reasonable.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

LO2 Identify components of workflow analysis.

Answered: 1 week ago