def specified_tag_greater_than_count( chosen_tag, checking_function, expected_count, given_file, containing_directory, exact=False, ): """Determine if the tag count is greater than expected in given file(s).""" # Use these two variables to keep track of tag counts for multiple files. # The idea is that file_tags_count_dictionary will store (key, value) pairs # where the key is the file and the count is the number of entities in that file. file_tags_count = 0 file_tags_count_dictionary = {} # Create a Path object to the chosen file in the containing directory, accounting # for the fact that a wildcard like "*.md" will create multiple paths. Note that # the create_paths function can only return valid paths, regardless of input. for file_for_checking in files.create_paths( file=given_file, home=containing_directory ): file_tag_count = 0 # since the specified file must be valid and thus suitable for checking, # read the contents of the file and then check for the chosen tag file_contents = file_for_checking.read_text() file_tag_count = checking_function(file_contents, chosen_tag) file_tags_count_dictionary[file_for_checking.name] = file_tag_count # return the minimum value and the entire dictionary of counts minimum_pair = util.get_first_minimum_value(file_tags_count_dictionary) file_tags_count = minimum_pair[1] # check the condition and also return file_tags_count return ( util.greater_than_equal_exacted(file_tags_count, expected_count, exact), file_tags_count_dictionary, )
def count_lines( given_file=constants.markers.Nothing, containing_directory=constants.markers.Nothing, contents=constants.markers.Nothing, ): """Count lines for the file in the directory, or alternatively, provided contents.""" # Use these two variables to keep track of line counts for multiple files. # The idea is that file_contents_count_dictionary will store (key, value) pairs # where the key is the file and the count is the number of entities in that file. file_contents_count = 0 file_contents_count_dictionary = {} # the contents are provided and thus there is no file or directory # the context for this condition is when the function checks # the output from the execution of a specified command if contents is not constants.markers.Nothing: line_list = get_line_list(contents) file_contents_count = len(line_list) # file is and directory are available and thus there are no contents # the context for this condition is when the function checks # the number of lines in a specific file in a specific directory elif (given_file is not constants.markers.Nothing and containing_directory is not constants.markers.Nothing): # Create a Path object to the chosen file in the containing directory, accounting # for the fact that a wildcard like "*.md" will create multiple paths. Note that # the create_paths function can only return valid paths, regardless of input. for file_for_checking in files.create_paths(file=given_file, home=containing_directory): file_contents_count = 0 # file is available and the contents are not provided # the context for this condition is when the function checks # the contents of a specified file that exists on the filesystem file_contents = file_for_checking.read_text() line_list = get_line_list(file_contents) file_contents_count = len(line_list) file_contents_count_dictionary[ file_for_checking.name] = file_contents_count # return the minimum value and the entire dictionary of counts minimum_pair = util.get_first_minimum_value( file_contents_count_dictionary) file_contents_count = minimum_pair[1] return file_contents_count, file_contents_count_dictionary
def count_entities( chosen_fragment, checking_function, given_file=constants.markers.Nothing, containing_directory=constants.markers.Nothing, contents=constants.markers.Nothing, ): """Count fragments for the file in the directory (or contents) and a fragment.""" # Use these two variables to keep track of entity counts for multiple files. # The idea is that file_contents_count_dictionary will store (key, value) pairs # where the key is the file and the count is the number of entities in that file. file_contents_count = 0 file_contents_count_dictionary = {} # file is not available and the contents are provided # the context for this condition is when the function checks # the output from the execution of a specified command if (contents is not constants.markers.Nothing and given_file is constants.markers.Nothing): # The command ran and produced an error, which means that its output # is technically "" or Nothing. So, set it to Nothing so that the # checking_function can determine that none of the entity exists in it if contents is constants.markers.Command_Error: contents = constants.markers.Nothing # run the checking_function to look for fragments in the contents file_contents_count = checking_function(contents, chosen_fragment) return file_contents_count, file_contents_count_dictionary for file_for_checking in files.create_paths(file=given_file, home=containing_directory): # an actual file is available and command contents are not provided # the context for this condition is when the function checks file contents # read the text from the file and then check for the chosen fragment file_contents = file_for_checking.read_text() file_contents_count = checking_function(file_contents, chosen_fragment) file_contents_count_dictionary[ file_for_checking.name] = file_contents_count # return the minimum value and the entire dictionary of counts minimum_pair = util.get_first_minimum_value(file_contents_count_dictionary) file_contents_count = minimum_pair[1] return file_contents_count, file_contents_count_dictionary
def test_find_minimum_in_dictionary_multiple_min(): """Check if the minimum value is found in a dictionary.""" input = {"John": 21, "Mike": 52, "Sarah": 12, "Bob": 43, "Gina": 12} found_values = util.get_first_minimum_value(input) assert found_values[0] == "Sarah" assert found_values[1] == 12
def test_find_in_empty_dictionary_min(): """Check if the None value is found in an empty dictionary.""" input = {} found_values = util.get_first_minimum_value(input) assert found_values[0] == 0 assert found_values[1] == 0