Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the language is python this is my count.py python code import sys import string #this function will add frequency counts to dictionary d #d: dictionary

image text in transcribedthe language is python

this is my count.py python code

import sys import string

#this function will add frequency counts to dictionary d #d: dictionary , map characters to frequency #files: source of input #remove_case: boolean def add_frequencies(d,file,remove_case):

textfile=file.read() #a list including all letters[a..z..A..Z] allLetters=list(string.ascii_letters)

#all lowercase ignore cases if remove_case: textfile=textfile.lower() allLetters=list(string.ascii_lowercase) #map characters to frequency for l in allLetters: if l in d: d[l]+=textfile.count(l) else: d[l]=textfile.count(l) return d

#parsing arguments and calling function add_frequencies #receive dictionary of mapped frequency #outputs in CSV format

def main(): d={} #checking if command '-c' exist remove_case = '-c' not in sys.argv #checking if command '-z' exist allLetters = '-z' in sys.argv

allLetter_filter=[] #checking if command '-l' exist; if exist, take allLetters and assign to allLetter_filter if '-l' in sys.argv: index= sys.argv.index('-l')+1 allLetter_filter=list(sys.argv[index].lower()) #for looping second argument for arguments in sys.argv[1:]: if arguments.endswith('.txt'): with open(arguments) as file: d = add_frequencies(d,file,remove_case)

#if command '-c' exist if not remove_case: for key in list(string.ascii_lowercase): if d[key] != 0: print(f"{key},{d[key]}") for key in list(string.ascii_uppercase): if d[key] != 0: print(f"{key},{d[key]}") #if command '-z' exist elif allLetters: for key in list(string.ascii_lowercase): print(f"{key},{d[key]}") #if command '-l' exist elif allLetter_filter: for key in letters_filter: print(f"{key},{d[key]}") #no command exist else: for key in list(string.ascii_lowercase): if d[key] != 0: print(f"{key},{d[key]}")

main()

What to do 1. Modularize Convert your program to a module. At the bottom of the file, put in the necessary code to call the main function if the code is not being imported. Now that it can be imported as a module without executing all the code, break out the core functionality that used to be in main() of this program into a separate function. You may modify this function so that it returns the dictionary rather than printing it. 2. Write to csv file In a separate file called count_to_csv.py, import your count.py. This script will take an additional argument over count.py: the final argument should be the name of a csv file to which you will write the data printed. This file does not need to already exist. For example: python3 count_to_csv.py -z -c test1.txt test2.txt out.csv 3. Unit test Create another file, test_count.py. This should contain the unit testing code for count.py. You do not need to unit test count_to_csv.py. Make sure to write test cases for every combination of flags. Hint: unittest.main() accepts an optional argv parameter, so you can hard code your parameter lists instead of passing them from the command line. What to do 1. Modularize Convert your program to a module. At the bottom of the file, put in the necessary code to call the main function if the code is not being imported. Now that it can be imported as a module without executing all the code, break out the core functionality that used to be in main() of this program into a separate function. You may modify this function so that it returns the dictionary rather than printing it. 2. Write to csv file In a separate file called count_to_csv.py, import your count.py. This script will take an additional argument over count.py: the final argument should be the name of a csv file to which you will write the data printed. This file does not need to already exist. For example: python3 count_to_csv.py -z -c test1.txt test2.txt out.csv 3. Unit test Create another file, test_count.py. This should contain the unit testing code for count.py. You do not need to unit test count_to_csv.py. Make sure to write test cases for every combination of flags. Hint: unittest.main() accepts an optional argv parameter, so you can hard code your parameter lists instead of passing them from the command line

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

5 Explain Complexity, Autonomy and Novelty.

Answered: 1 week ago