Question
Please help me with writing argparse to implement the CLI portion of the program, including the testing of both passing and failing functions. HW3_test.py is
Please help me with writing argparse to implement the CLI portion of the program, including the testing of both passing and failing functions. HW3_test.py is the CLI program skeleton that imports the functions of HW3.py. HW3.py contains all the functions, which are completed based on the test in REPL. The output of the program is supposed to look like the following when using -h help flag:
$ python HW3_test.py -h usage: HW3_test.py [-h] [-u] [-w] [-l]
optional arguments:
-h, --help show this help message and exit
-u, --unique Flag to test the unique function from HW3
-w, --words Flag to test the words_containing function from HW3
-l, --len Flag to test the len_test function from HW3
When running HW3_test.py, the flags giving in any combiniation to the passing functions can output the following.
When you put an invalid flag, argparse should print out an error.
$ python HW3_test.py -w
words_containing passed
$ python HW3_test.py -u -l
unique passed
len_test passed
$ python HW3_test.py -r
usage: HW3_test.py [-h] [-u] [-w] [-l]
HW3_test.py: error: unrecognized arguments: -r
When being tested with failing functions, it should print the following:
$ python HW3_test.py -w
words_containing failed
HW3.py
""" Test in the REPL:
>>> from HW3 import words_containing
>>> sentence = "Anyone who has never made a mistake has never tried anything new"
>>> words_containing(sentence, 'a')
# Should return:
['Anyone', 'has', 'made', 'a', 'mistake', 'has', 'anything']
>>> words_containing(sentence, 'x')
# Should return:
[]
"""
def words_containing(sentence, letter):
letterslist=[]
wordslist=[]
for word in sentence.split():
wordslist.append(word)
for element in wordslist:
if set(letter) & set(element):
letterslist.append(element)
return letterslist
""" Test in the REPL:
>>> from HW3 import len_test
>>> my_dict = {'a': 23, 'b': 8}
>>> print(len_test(my_dict))
# should return:
2
>>> print(len_test(7))
# should return:
-1
>>> print(len_test(""))
# should return:
0
"""
def len_test(obj):
obj_length=0
try:
obj_length=len(obj)
except TypeError:
obj_length=-1
return obj_length
""" Test in the REPL:
>>> from HW3 import unique
>>> numbers = [4, 5, 2, 6, 2, 3, 5, 8]
>>> nums = unique(numbers)
>>> next(nums)
# should return:
4
>>> next(nums)
5
>>> next(nums)
2
>>> next(nums)
6
>>> next(nums)
3
>>> next(nums)
8
>>> next(nums)
Traceback (most recent call last):
File "
StopIteration
>>>
You can also test it quickly with:
>>> list(unique(numbers))
# should return:
[4, 5, 2, 6, 3, 8]
"""
def unique(iterable):
output=[]
for x in iterable:
if x not in output:
output.append(x)
return iter(output)
HW3_test.py
import argparse
from HW3 import words_containing, len_test, unique
def test_unique():
numbers = [4,5,2,6,2,3,5,8]
unique_nums= [4,5,2,6,3,8]
if unique(numbers) == unique_nums :
return true
if len(unique(numbers)) == 6
return true
else:
return false
def test_words_containing():
sentence = "Anyone who has never made a mistake has never tried anything new"
output1 = ['Anyone', 'has', 'made', 'a', 'mistake', 'has', 'anything']
output2=[]
if unique(sentence, 'a') == output1:
return true
if unique(sentence, 'u')== output2:
return true
else:
return false
def test_len_test():
my_dict=('a':23,'b':8}
if len_test(my_dict) == 2:
return true
elif len_test(7)) == -1:
return true
elif len_test("") == 0:
return true
else:
return flase
def start_my_program():
if __name__ == "__main__":
start_my_parogram()
# Set up argparse information here
# Based on user input, run test(s) requested and report outcome
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started