Skip to content

Commit d3f69d1

Browse files
Refactor: Move process_query to top and prefix helper functions with an underscore
1 parent 856a822 commit d3f69d1

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

src/process_query.py

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,6 @@
1111
templates = Jinja2Templates(directory="templates")
1212

1313

14-
def print_query(url: str, max_file_size: int, pattern_type: str, pattern: str) -> None:
15-
"""
16-
Print a formatted summary of the query details, including the URL, file size,
17-
and pattern information, for easier debugging or logging.
18-
19-
Parameters
20-
----------
21-
url : str
22-
The URL associated with the query.
23-
max_file_size : int
24-
The maximum file size allowed for the query, in bytes.
25-
pattern_type : str
26-
Specifies the type of pattern to use, either "include" or "exclude".
27-
pattern : str
28-
The actual pattern string to include or exclude in the query.
29-
"""
30-
print(f"{Colors.WHITE}{url:<20}{Colors.END}", end="")
31-
if int(max_file_size / 1024) != 50:
32-
print(f" | {Colors.YELLOW}Size: {int(max_file_size/1024)}kb{Colors.END}", end="")
33-
if pattern_type == "include" and pattern != "":
34-
print(f" | {Colors.YELLOW}Include {pattern}{Colors.END}", end="")
35-
elif pattern_type == "exclude" and pattern != "":
36-
print(f" | {Colors.YELLOW}Exclude {pattern}{Colors.END}", end="")
37-
38-
39-
def print_error(url: str, e: Exception, max_file_size: int, pattern_type: str, pattern: str) -> None:
40-
"""
41-
Print a formatted error message including the URL, file size, pattern details, and the exception encountered,
42-
for debugging or logging purposes.
43-
44-
Parameters
45-
----------
46-
url : str
47-
The URL associated with the query that caused the error.
48-
e : Exception
49-
The exception raised during the query or process.
50-
max_file_size : int
51-
The maximum file size allowed for the query, in bytes.
52-
pattern_type : str
53-
Specifies the type of pattern to use, either "include" or "exclude".
54-
pattern : str
55-
The actual pattern string to include or exclude in the query.
56-
"""
57-
print(f"{Colors.BROWN}WARN{Colors.END}: {Colors.RED}<- {Colors.END}", end="")
58-
print_query(url, max_file_size, pattern_type, pattern)
59-
print(f" | {Colors.RED}{e}{Colors.END}")
60-
61-
62-
def print_success(url: str, max_file_size: int, pattern_type: str, pattern: str, summary: str) -> None:
63-
"""
64-
Print a formatted success message, including the URL, file size, pattern details, and a summary with estimated
65-
tokens, for debugging or logging purposes.
66-
67-
Parameters
68-
----------
69-
url : str
70-
The URL associated with the successful query.
71-
max_file_size : int
72-
The maximum file size allowed for the query, in bytes.
73-
pattern_type : str
74-
Specifies the type of pattern to use, either "include" or "exclude".
75-
pattern : str
76-
The actual pattern string to include or exclude in the query.
77-
summary : str
78-
A summary of the query result, including details like estimated tokens.
79-
"""
80-
estimated_tokens = summary[summary.index("Estimated tokens:") + len("Estimated ") :]
81-
print(f"{Colors.GREEN}INFO{Colors.END}: {Colors.GREEN}<- {Colors.END}", end="")
82-
print_query(url, max_file_size, pattern_type, pattern)
83-
print(f" | {Colors.PURPLE}{estimated_tokens}{Colors.END}")
84-
85-
8614
async def process_query(
8715
request: Request,
8816
input_text: str,
@@ -149,7 +77,7 @@ async def process_query(
14977
except Exception as e:
15078
# hack to print error message when query is not defined
15179
if "query" in locals() and query is not None and isinstance(query, dict):
152-
print_error(query["url"], e, max_file_size, pattern_type, pattern)
80+
_print_error(query["url"], e, max_file_size, pattern_type, pattern)
15381
else:
15482
print(f"{Colors.BROWN}WARN{Colors.END}: {Colors.RED}<- {Colors.END}", end="")
15583
print(f"{Colors.RED}{e}{Colors.END}")
@@ -173,7 +101,7 @@ async def process_query(
173101
"download full ingest to see more)\n" + content[:MAX_DISPLAY_SIZE]
174102
)
175103

176-
print_success(
104+
_print_success(
177105
url=query["url"],
178106
max_file_size=max_file_size,
179107
pattern_type=pattern_type,
@@ -197,3 +125,75 @@ async def process_query(
197125
"pattern": pattern,
198126
},
199127
)
128+
129+
130+
def _print_query(url: str, max_file_size: int, pattern_type: str, pattern: str) -> None:
131+
"""
132+
Print a formatted summary of the query details, including the URL, file size,
133+
and pattern information, for easier debugging or logging.
134+
135+
Parameters
136+
----------
137+
url : str
138+
The URL associated with the query.
139+
max_file_size : int
140+
The maximum file size allowed for the query, in bytes.
141+
pattern_type : str
142+
Specifies the type of pattern to use, either "include" or "exclude".
143+
pattern : str
144+
The actual pattern string to include or exclude in the query.
145+
"""
146+
print(f"{Colors.WHITE}{url:<20}{Colors.END}", end="")
147+
if int(max_file_size / 1024) != 50:
148+
print(f" | {Colors.YELLOW}Size: {int(max_file_size/1024)}kb{Colors.END}", end="")
149+
if pattern_type == "include" and pattern != "":
150+
print(f" | {Colors.YELLOW}Include {pattern}{Colors.END}", end="")
151+
elif pattern_type == "exclude" and pattern != "":
152+
print(f" | {Colors.YELLOW}Exclude {pattern}{Colors.END}", end="")
153+
154+
155+
def _print_error(url: str, e: Exception, max_file_size: int, pattern_type: str, pattern: str) -> None:
156+
"""
157+
Print a formatted error message including the URL, file size, pattern details, and the exception encountered,
158+
for debugging or logging purposes.
159+
160+
Parameters
161+
----------
162+
url : str
163+
The URL associated with the query that caused the error.
164+
e : Exception
165+
The exception raised during the query or process.
166+
max_file_size : int
167+
The maximum file size allowed for the query, in bytes.
168+
pattern_type : str
169+
Specifies the type of pattern to use, either "include" or "exclude".
170+
pattern : str
171+
The actual pattern string to include or exclude in the query.
172+
"""
173+
print(f"{Colors.BROWN}WARN{Colors.END}: {Colors.RED}<- {Colors.END}", end="")
174+
_print_query(url, max_file_size, pattern_type, pattern)
175+
print(f" | {Colors.RED}{e}{Colors.END}")
176+
177+
178+
def _print_success(url: str, max_file_size: int, pattern_type: str, pattern: str, summary: str) -> None:
179+
"""
180+
Print a formatted success message, including the URL, file size, pattern details, and a summary with estimated
181+
tokens, for debugging or logging purposes.
182+
183+
Parameters
184+
----------
185+
url : str
186+
The URL associated with the successful query.
187+
max_file_size : int
188+
The maximum file size allowed for the query, in bytes.
189+
pattern_type : str
190+
Specifies the type of pattern to use, either "include" or "exclude".
191+
pattern : str
192+
The actual pattern string to include or exclude in the query.
193+
summary : str
194+
A summary of the query result, including details like estimated tokens.
195+
"""
196+
estimated_tokens = summary[summary.index("Estimated tokens:") + len("Estimated ") :]
197+
print(f"{Colors.GREEN}INFO{Colors.END}: {Colors.GREEN}<- {Colors.END}", end="")
198+
_print_query(url, max_file_size, pattern_type, pattern)
199+
print(f" | {Colors.PURPLE}{estimated_tokens}{Colors.END}")

0 commit comments

Comments
 (0)