Skip to content

Keyword finder

SearchResult dataclass

Class to hold the results of a file search. file_path: The path to the file that was searched. word_count: A dictionary containing the number of times each keyword was found in the file.

Source code in quinn/keyword_finder.py
57
58
59
60
61
62
63
64
65
@dataclass
class SearchResult:
    """Class to hold the results of a file search.
    file_path: The path to the file that was searched.
    word_count: A dictionary containing the number of times each keyword was found in the file.
    """

    file_path: str
    word_count: dict[str, int]

keyword_format(input, keywords=default_keywords)

Formats the input string to highlight the keywords.

Parameters:

Name Type Description Default
input str

The string to format.

required
keywords list[str]

The list of keywords to highlight.

default_keywords
Source code in quinn/keyword_finder.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def keyword_format(input: str, keywords: list[str] = default_keywords) -> str:
    """Formats the input string to highlight the keywords.

    :param input: The string to format.
    :type input: str
    :param keywords: The list of keywords to highlight.
    :type keywords: list[str]

    """
    nc = "\033[0m"
    red = "\033[31m"
    bold = "\033[1m"
    res = input
    for keyword in keywords:
        res = surround_substring(res, keyword, red + bold, nc)
    return res

search_file(path, keywords=default_keywords)

Searches a file for keywords and prints the line number and line containing the keyword.

Parameters:

Name Type Description Default
path str

The path to the file to search.

required
keywords list[str]

The list of keywords to search for.

default_keywords

Returns:

Type Description
SearchResult

A dictionary containing a file path and the number of lines containing a keyword in keywords.

Source code in quinn/keyword_finder.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def search_file(path: str, keywords: list[str] = default_keywords) -> SearchResult:
    """Searches a file for keywords and prints the line number and line containing the keyword.

    :param path: The path to the file to search.
    :type path: str
    :param keywords: The list of keywords to search for.
    :type keywords: list[str]
    :returns: A dictionary containing a file path and the number of lines containing a keyword in `keywords`.
    :rtype: SearchResult

    """
    match_results = SearchResult(file_path=path, word_count={keyword: 0 for keyword in keywords})

    print(f"\nSearching: {path}")
    with open(path) as f:
        for line_number, line in enumerate(f, 1):
            line_printed = False
            for keyword in keywords:
                if keyword in line:
                    match_results.word_count[keyword] += 1

                    if not line_printed:
                        print(f"{line_number}: {keyword_format(line)}", end="")
                        line_printed = True

    return match_results

search_files(path, keywords=default_keywords)

Searches all files in a directory for keywords.

Parameters:

Name Type Description Default
path str

The path to the directory to search.

required
keywords list[str]

The list of keywords to search for.

default_keywords

Returns:

Type Description
list[SearchResult]

A list of dictionaries containing file paths and the number of lines containing a keyword in keywords.

Source code in quinn/keyword_finder.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def search_files(path: str, keywords: list[str] = default_keywords) -> list[SearchResult]:
    """Searches all files in a directory for keywords.

    :param path: The path to the directory to search.
    :type path: str
    :param keywords: The list of keywords to search for.
    :type keywords: list[str]
    :returns: A list of dictionaries containing file paths and the number of lines containing a keyword in `keywords`.
    :rtype: list[SearchResult]

    """
    rootdir_glob = f"{path}/**/*"
    file_list = [f for f in iglob(rootdir_glob, recursive=True) if os.path.isfile(f)]
    return [search_file(f, keywords) for f in file_list]

surround_substring(input, substring, surround_start, surround_end)

Surrounds a substring with the given start and end strings.

Parameters:

Name Type Description Default
input str

The string to search.

required
substring str

The substring to surround.

required
surround_start str

The string to start the surrounding with.

required
surround_end str

The string to end the surrounding with.

required

Returns:

Type Description
str

The input string with the substring surrounded.

Source code in quinn/keyword_finder.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def surround_substring(input: str, substring: str, surround_start: str, surround_end: str) -> str:
    """Surrounds a substring with the given start and end strings.

    :param input: The string to search.
    :type input: str
    :param substring: The substring to surround.
    :type substring: str
    :param surround_start: The string to start the surrounding with.
    :type surround_start: str
    :param surround_end: The string to end the surrounding with.
    :type surround_end: str
    :returns: The input string with the substring surrounded.
    :rtype: str

    """
    return input.replace(
        substring,
        surround_start + substring + surround_end,
    )