def invoke_all_minimum_word_count_checks(filecheck, directory, expected_count, count_function, conclusion, exact=False): """Perform the word count check and return the results.""" met_or_exceeded_count = 0 ( met_or_exceeded_count, actual_count, actual_count_dictionary, ) = entities.entity_greater_than_count(filecheck, directory, expected_count, count_function, exact) # create the message and the diagnostic if not exact: # create an "at least" message, which is the default message = ("The " + filecheck + " in " + directory + " has at least " + str(expected_count) + constants.markers.Space + conclusion) else: # create an "exact" message, which is an opt-in message = ("The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + constants.markers.Space + conclusion) # create a diagnostic message and report the result # replace "in every" with "in a" and a specific paragraph number. # This diagnostic signals the fact that there was at least # a single paragraph that had a word count below the standard # set for all of the paragraphs in the technical writing # across all of the files specified (i.e., those matched by wildcards) word_diagnostic, filename = util.get_word_diagnostic( actual_count_dictionary, expected_count) # there is no need for a filename diagnostic unless there are multiple results filename_diagnostic = constants.markers.Nothing # there is a filename, which means that there was a wildcard specified # and thus this diagnostic is for one file; give name at the end if filename: filename_diagnostic = (constants.markers.Of_File + constants.markers.Space + filename) # since there is a word_diagnostic, this means that there is a need to customize # the diagnostic message because the check is not going to pass correctly if word_diagnostic: conclusion = conclusion.replace(constants.words.In_Every, word_diagnostic) # the actual_count may vary depending on whether the check is checking for exact # equality or if there is a minimum threshold that the inputs must satisfy # --> exactness is not required, so find the minimum value across all inputs if not exact: actual_count = util.get_first_minimum_value_deep( actual_count_dictionary)[1][1] # --> exactness is required, so find the first value that does not match the specified value elif exact: actual_count = util.get_first_not_equal_value_deep( actual_count_dictionary, expected_count)[1][1] # create the diagnostic message using all of the parts, specifically highlighting # the ways in which the check failed, thereby improving a person's debugging process diagnostic = ("Found " + str(actual_count) + constants.markers.Space + conclusion + constants.markers.Space + filename_diagnostic) report_result(met_or_exceeded_count, message, diagnostic) return met_or_exceeded_count
def count_entities(given_file, containing_directory, checking_function): """Count the number of entities for the file(s) in the directory.""" # create an empty dictionary of filenames and an internal dictionary file_counts_dictionary = {} # if the file is not found in the directory, the count is zero file_contents_count = 0 # create a path for the given file and its containing directory # note that this call does not specify any *args and thus there # are no directories between the home directory and the file for file_for_checking in files.create_paths( file=given_file, home=containing_directory ): # start the count of the number of entities at zero, assuming none found yet file_contents_count = 0 # create an empty dictionary of the counts file_contents_count_dictionary = {} # a valid file exists and thus it is acceptable to perform the checking # extract the text from the file_for_checking file_contents = file_for_checking.read_text() # use the provided checking_function to check the contents of the file # note this works since Python supports passing a function to a function file_contents_count, file_contents_count_dictionary = checking_function( file_contents ) # the checking_function returned a dictionary of form {entity: count} # so we should store this dictionary insider the containing dictionary # this case would occur for checks like number of words in paragraphs if file_contents_count_dictionary: # associate these file counts with the filename in a dictionary file_counts_dictionary[ file_for_checking.name ] = file_contents_count_dictionary # the checking_function did not return a dictionary because that was # not sensible for the type of check (e.g., counting paragraphs) # so we should make a "dummy" dictionary containing the entity count else: file_contents_count_dictionary = {1: file_contents_count} file_counts_dictionary[ file_for_checking.name ] = file_contents_count_dictionary # find the minimum count for all paragraphs across all of the files # assume that nothing was found and the count is zero and prove otherwise file_contents_count_overall = file_contents_count # there is a dictionary of counts for files, so deeply find the minimum # as long as the count is not of the total words in a file if ( file_counts_dictionary and checking_function.__name__ != constants.functions.Count_Total_Words ): file_contents_count_overall = util.get_first_minimum_value_deep( file_counts_dictionary )[1][1] # return the overall requested count and the nested file count dictionary return file_contents_count_overall, file_counts_dictionary
def test_find_minimum_in_dictionary_single_max_deep_words(): """Check if the minimum value is found in a dictionary deep.""" input_file_one = {1: 10, 2: 5, 3: 4} input_file_two = {1: 10, 2: 5, 3: 4} input_file_three = {1: 10, 2: 5, 3: 1} outer_dictionary = { "input_file_one": input_file_one, "input_file_two": input_file_two, "input_file_three": input_file_three, } found = util.get_first_minimum_value_deep(outer_dictionary) assert found[0] == "input_file_three" assert found[1] == (3, 1)
def invoke_all_comment_checks(filecheck, directory, expected_count, comment_type, language, exact=False): """Perform the comment check and return the results.""" met_or_exceeded_count = 0 actual_count = 0 comment_count_details = {} # check single-line comments if comment_type == constants.comments.Single_Line: # check comments in Java if language == constants.languages.Java: met_or_exceeded_count, actual_count, comment_count_details = entities.entity_greater_than_count( filecheck, directory, expected_count, comments.count_singleline_java_comment, exact, ) # check comments in Python if language == constants.languages.Python: met_or_exceeded_count, actual_count, comment_count_details = entities.entity_greater_than_count( filecheck, directory, expected_count, comments.count_singleline_python_comment, exact, ) # check multiple-line comments elif comment_type == constants.comments.Multiple_Line: # check comments in Java if language == constants.languages.Java: met_or_exceeded_count, actual_count, comment_count_details = entities.entity_greater_than_count( filecheck, directory, expected_count, comments.count_multiline_java_comment, exact, ) # check comments in Python if language == constants.languages.Python: met_or_exceeded_count, actual_count, comment_count_details = entities.entity_greater_than_count( filecheck, directory, expected_count, comments.count_multiline_python_comment, exact, ) # check comments in a not-supported language # currently the only valid options are: # --> single-line # --> multiple-line # this means that this check will fail because # it will not find any of the specified comments else: pass # create the message and the diagnostic if not exact: # create an "at least" message, which is the default message = ("The " + filecheck + " in " + directory + " has at least " + str(expected_count) + constants.markers.Space + comment_type + constants.markers.Space + language + " comment(s)") else: # create an "exact" message, which is an opt-in message = ("The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + constants.markers.Space + comment_type + constants.markers.Space + language + " comment(s)") # --> exactness is not required, so find the first minimum value if not exact: actual_count = util.get_first_minimum_value_deep(comment_count_details) if actual_count != (0, 0): actual_count = actual_count[1][1] else: actual_count = 0 # get the "most minimal" actual_count from the flattened report from previously run check fragment_diagnostic, fragment_count = util.get_file_diagnostic_deep_not_exact( comment_count_details) # --> exactness is required, so find the first value that does not match the specified value elif exact: fragment_diagnostic, fragment_count = util.get_file_diagnostic_deep_exact( comment_count_details, expected_count) new_actual_count = util.get_first_not_equal_value_deep( comment_count_details, expected_count) if new_actual_count == {}: new_actual_count = util.get_first_not_equal_value( comment_count_details, expected_count) if new_actual_count != (0, 0): new_actual_count = new_actual_count[1][1] if new_actual_count != actual_count: met_or_exceeded_count = False actual_count = new_actual_count diagnostic = ("Found " + str(actual_count) + constants.markers.Space + "comment(s)" + constants.markers.Space + fragment_diagnostic + constants.markers.Space + "or the output") report_result(met_or_exceeded_count, message, diagnostic) return met_or_exceeded_count