Consider the Python script randline.py Write a new script shuf.py in the style o
ID: 3715670 • Letter: C
Question
Consider the Python script randline.py
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. 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 implementation should support the following shuf options, with the same behavior as GNU shuf: --echo (-e), --head-count (-n), --repeat (-r), and --help. It should also support any number (including zero) of non-option arguments, as well as the argument "-" meaning standard input. 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 implementation of shuf.py 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.
Explanation / Answer
Here is the implimentation of shuf.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 <http://www.gnu.org/licenses/> for a copy of the license.
$Id: randline.py,v 1.4 2010/04/05 20:04:43 eggert Exp $
"""
import random, sys
import argparse
def main():
version_msg = "%prog 2.0"
usage_msg = """%prog [OPTION]... FILE
Output randomly shuffled lines from FILE or CL input."""
parser = argparse.ArgumentParser(description='shuffle the input.')
parser.add_argument("-n", "--numlines",
action="store", dest="numlines",
help="output at most COUNT lines")
parser.add_argument("-e", "--echo", dest="echolines", default=None, nargs='+',
action="store", help="treat each ARG as an input line")
parser.add_argument("-r", "--repeat", dest="repeat", default=False, const=True,
action="store_const", help="whether to shuffle with replacement or without")
args = parser.parse_args()
input_lines = args.echolines #input lines
try:
numlines = int(args.numlines)
except:
numlines = len(input_lines)
if numlines < 0:
parser.error("negative count: {0}".
format(numlines))
if args.repeat:
for i in range(numlines):
line=random.choice(input_lines)
print(line)
else:
lines=random.sample(input_lines, numlines)
for line in lines:
print(line)
if __name__ == "__main__":
main()
Use python shuf.py --help to know how the command works.
here the repeat and non-repeat are implimented using random.choice and random.sample respectively.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.