human
Complete this python program:
def _count_valid_files_in_directory(directory, white_list_formats, follow_links):
assistant
#Here is the Python code you requested.

    """Count files with extension in `white_list_formats` contained in a directory.
    # Arguments
    directory: absolute path to the directory containing files to be counted
    white_list_formats: set of strings containing allowed extensions for
    the files to be counted.
    # Returns
    the count of files with extension in `white_list_formats` contained in
    the directory.
    """
    def _recursive_list(subpath):
        return sorted(os.walk(subpath, followlinks=follow_links), key=(lambda tpl: tpl[0]))
    samples = 0
    for (root, _, files) in _recursive_list(directory):
        for fname in files:
            is_valid = False
            for extension in white_list_formats:
                if fname.lower().endswith(('.' + extension)):
                    is_valid = True
                    break
            if is_valid:
                samples += 1
    return samples