Question
About unit testing import csv import sys import string def add_frequencies(dict,file,remove_case): if remove_case == True: for i in file: if len(i) != 1: for j
About unit testing
import csv
import sys import string
def add_frequencies(dict,file,remove_case): if remove_case == True: for i in file: if len(i) != 1: for j in i: if j.lower() not in dict.keys(): dict[j.lower()]=1 else: dict[j.lower()]+=1 else: if i.lower() not in dict.keys(): dict[i.lower()]=1 else: dict[i.lower()]+=1 else: for i in file: if len(i) != 1: for j in i: if j not in dict.keys(): dict[j]=1 else: dict[j]+=1 return dict def main(): characters = string.ascii_letters remove_case = True print_zeroes = False
args = sys.argv[1:] while args and args[0].startswith('-'): arg = args.pop(0)
if arg == '-c': remove_case = False elif arg == '-l': characters = args.pop(0) elif arg == '-z': print_zeroes = True elif arg == '--': break else: print(f'unknown argument: \'{arg}\'', file=sys.stderr)
if remove_case: characters = ''.join(i for i in characters if i.islower())
dict = {} for filename in args: with open(filename, 'r') as file: add_frequencies(dict, file, remove_case)
for c in characters: l = dict[c] if c in dict else 0 if l != 0 or print_zeroes: print(f'"{c}",{l}')
for csvname in args: with open(csvname,'w') as new_file: writer = csv.writer(new_file)
if __name__ == '__main__': main()
This function is used to read the file, and detect the frequency of each character in the string, run in cmd, and output the result
1.I need to use unit tests to test the above code, but I don't know how to do it. I want to know how to write unit test code to test my function
2. I want to import the above functions as modules into a new file and add the new functions to the new file. This function is used to create the output of the first function as a CSV file, and the name of this CSV file depends on what I input on cmd. For example, [python test.py -c test02] can make the above function output results. The function of the new function is that if I enter [python test.py -c test neos], neos will be the file name of the new CSV file to be output. This is my code, but I cannot output the required information, so I need help.
import countt import sys import csv def main(argv): with open('countt.py','r') as csv_file: read = csv.reader(csv_file) args = countt.main(argv[-1]) with open(args,'w') as new_file: writer = csv.writer(new_file) for line in read: writer.writerow(line) if __name__ == '__main__': countt.main()
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