Answered You can hire a professional tutor to get the answer.
AUTOMATED TESTING QUESTION: CAN YOU WRITE automated tests for the three non-main functions: get_file_lines, fine_in_file and report_results.
AUTOMATED TESTING
- QUESTION:
CAN YOU WRITE automated tests for the three non-main functions: get_file_lines, fine_in_file and report_results.PERFORM actual pytest functions and attach the .py file to your exam (including it in a zip file would be fine)
def get_file_lines(name):
'''
Open a file, read the lines in and return the file contents
:param name: the name of the file to load (assumes fully qualified name)
:return: a list of strings with all the lines in the file
'''
try:
file = open(name, encoding='UTF-8') # Mac and linux use UTF-8 by default, but windows barfs unless we make it explicit
lines = file.readlines()
return lines
except OSError as f_error:
print(f"error opening file with name {name}, message: {str(f_error)}")
return []
def find_in_file(keywords, all_lines):
results = {}
line_number = 1 #adjusting for python courts from zero
# question for the exam - what is wrong with this naive implementation?
for line in all_lines:
for word in keywords:
if word in line:
line_name = f"line {line_number}:"
results[line_name] = [word, line]
break
line_number+=1
return results
def report_results(results, file=None):#second param to help in testing
for key in results:
value = results[key]
print(f"{key} {value[0]} found in {value[1]}", file= file)
def main():
'''in real life we would get command line arguments, but I don't want the exam
to hinge on if you have worked with command line arguments before, so I'll
just ask the user here, that will make the rest of the functions more testable.'''
file_name = input("What file should we look in:")
file_contents = get_file_lines(file_name)
keywords = input("what keywords do you want to search for (separated by spaces?")
keywords = keywords.split(" ")
results = find_in_file(keywords, file_contents)
report_results(results)
if __name__ == '__main__':
main()