Skip to content

Commit 60afba6

Browse files
committed
🎨 lazy logging + exe_info and Error messaging splitting (line length)
1 parent 6c66f42 commit 60afba6

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

src/vuegen/report.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ def read_network(self) -> nx.Graph:
220220
# Check if the file extension matches any Enum value
221221
if not any(file_extension == fmt.value_with_dot for fmt in NetworkFormat):
222222
self.logger.error(
223-
f"Unsupported file extension: {file_extension}. Supported extensions are: {', '.join(fmt.value for fmt in NetworkFormat)}."
223+
"Unsupported file extension: %s. Supported extensions are: %s",
224+
file_extension,
225+
", ".join(fmt.value for fmt in NetworkFormat),
224226
)
225227

226228
# Handle HTML files for pyvis interactive networks
@@ -240,7 +242,9 @@ def read_network(self) -> nx.Graph:
240242
df_net = pd.read_csv(file_stream, delimiter=delimiter)
241243
except pd.errors.ParserError:
242244
self.logger.error(
243-
f"Error parsing CSV/TXT file {self.file_path}. Please check the file format or delimiter."
245+
"Error parsing CSV/TXT file %s. "
246+
"Please check the file format or delimiter.",
247+
self.file_path,
244248
)
245249

246250
if self.csv_network_format == CSVNetworkFormat.EDGELIST:
@@ -252,7 +256,9 @@ def read_network(self) -> nx.Graph:
252256
required_columns.difference(df_net.columns)
253257
)
254258
self.logger.error(
255-
f"CSV network file must contain 'source' and 'target' columns. Missing columns: {missing_cols}."
259+
"CSV network file must contain 'source' and 'target' columns."
260+
" Missing columns: %s.",
261+
missing_cols,
256262
)
257263

258264
# Use additional columns as edge attributes,
@@ -275,31 +281,34 @@ def read_network(self) -> nx.Graph:
275281
)
276282

277283
self.logger.info(
278-
f"Successfully read network from file: {self.file_path}."
284+
"Successfully read network from file: %s.", self.file_path
279285
)
280286
return G
281287
elif self.csv_network_format == CSVNetworkFormat.ADJLIST:
282288
G = nx.from_pandas_adjacency(df_net)
283289
self.logger.info(
284-
f"Successfully read network from file: {self.file_path}."
290+
"Successfully read network from file: %s.", self.file_path
285291
)
286292
return G
287293
else:
288294
self.logger.error(
289-
f"Unsupported format for CSV/TXT file: {self.csv_network_format}."
295+
"Unsupported format for CSV/TXT file: %s.",
296+
self.csv_network_format,
290297
)
291298

292299
# Handle other formats using the mapping and return the NetworkX graph object
293300
# from the specified network file
294301
G = file_extension_map[file_extension](file_stream)
295302
G = self._add_size_attribute(G)
296-
self.logger.info(f"Successfully read network from file: {self.file_path}.")
303+
self.logger.info("Successfully read network from file: %s.", self.file_path)
297304
return G
298305
except Exception as e:
299-
self.logger.error(f"Error occurred while reading network file: {str(e)}.")
300-
raise RuntimeError(
301-
f"An error occurred while reading the network file: {str(e)}"
306+
self.logger.error(
307+
"Error occurred while reading network file: %s.", e, exc_info=True
302308
)
309+
raise RuntimeError(
310+
"An error occurred while reading the network file."
311+
) from e
303312

304313
def save_network_image(
305314
self, G: nx.Graph, output_file: str, format: str, dpi: int = 300
@@ -322,31 +331,36 @@ def save_network_image(
322331
# Check if the output file path is valid
323332
if not os.path.isdir(os.path.dirname(output_file)):
324333
self.logger.error(
325-
f"Directory for saving image does not exist: {os.path.dirname(output_file)}."
334+
"Directory for saving image does not exist: %s",
335+
os.path.dirname(output_file),
326336
)
327337
raise FileNotFoundError(
328-
f"The directory for saving the file does not exist: {os.path.dirname(output_file)}."
338+
"The directory for saving the file does not exist: "
339+
f"{os.path.dirname(output_file)}."
329340
)
330341

331342
# Validate image format
332343
valid_formats = ["png", "jpg", "jpeg", "svg"]
333344
if format.lower() not in valid_formats:
334345
self.logger.error(
335-
f"Invalid image format: {format}. Supported formats are: {', '.join(valid_formats)}."
346+
"Invalid image format: %s. Supported formats are: %s.",
347+
format,
348+
", ".join(valid_formats),
336349
)
337350
raise ValueError(
338-
f"Invalid format: {format}. Supported formats are: {', '.join(valid_formats)}."
351+
f"Invalid format: {format}."
352+
f" Supported formats are: {', '.join(valid_formats)}."
339353
)
340354

341355
try:
342356
# Draw the graph and save it as an image file
343357
nx.draw(G, with_labels=False)
344358
plt.savefig(output_file, format=format, dpi=dpi)
345359
plt.clf()
346-
self.logger.info(f"Network image saved successfully at: {output_file}.")
360+
self.logger.info("Network image saved successfully at: %s.", output_file)
347361
except Exception as e:
348-
self.logger.error(f"Failed to save the network image: {str(e)}.")
349-
raise RuntimeError(f"Failed to save the network image: {str(e)}")
362+
self.logger.error("Failed to save the network image: %s.", e, exc_info=True)
363+
raise RuntimeError("Failed to save the network image.") from e
350364

351365
def create_and_save_pyvis_network(self, G: nx.Graph, output_file: str) -> Network:
352366
"""
@@ -368,17 +382,19 @@ def create_and_save_pyvis_network(self, G: nx.Graph, output_file: str) -> Networ
368382
# Check if the network object and output file path are valid
369383
if not isinstance(G, nx.Graph):
370384
self.logger.error(
371-
f"Provided object is not a valid NetworkX graph: {type(G)}."
385+
"Provided object is not a valid NetworkX graph: %s.", type(G)
372386
)
373387
raise TypeError(
374388
f"The provided object is not a valid NetworkX graph: {type(G)}."
375389
)
376390
if not os.path.isdir(os.path.dirname(output_file)):
377391
self.logger.error(
378-
f"Directory for saving PyVis network does not exist: {os.path.dirname(output_file)}."
392+
"Directory for saving PyVis network does not exist: %s.",
393+
os.path.dirname(output_file),
379394
)
380395
raise FileNotFoundError(
381-
f"The directory for saving the file does not exist: {os.path.dirname(output_file)}."
396+
"The directory for saving the file does not exist: "
397+
f"{os.path.dirname(output_file)}."
382398
)
383399

384400
try:
@@ -409,12 +425,14 @@ def create_and_save_pyvis_network(self, G: nx.Graph, output_file: str) -> Networ
409425

410426
# Save the network as an HTML file
411427
net.save_graph(str(output_file))
412-
self.logger.info(f"PyVis network created and saved as: {output_file}.")
428+
self.logger.info("PyVis network created and saved as: %s.", output_file)
413429
return net
414430

415431
except Exception as e:
416-
self.logger.error(f"Failed to create and save PyVis network: {str(e)}.")
417-
raise RuntimeError(f"Failed to create and save the PyVis network: {str(e)}")
432+
self.logger.error(
433+
"Failed to create and save PyVis network: %s.", e, exc_info=True
434+
)
435+
raise RuntimeError("Failed to create and save the PyVis network.") from e
418436

419437
def _add_size_attribute(self, G: nx.Graph) -> nx.Graph:
420438
"""
@@ -617,9 +635,9 @@ def make_api_request(
617635
else self.request_body
618636
)
619637
try:
620-
self.logger.info(f"Making {self.method} request to API: {self.api_url}")
621-
self.logger.debug(f"Headers: {self.headers}")
622-
self.logger.debug(f"Params: {self.params}")
638+
self.logger.info("Making %s request to API: %s", self.method, self.api_url)
639+
self.logger.debug("Headers: %s", self.headers)
640+
self.logger.debug("Params: %s", self.params)
623641

624642
response = requests.request(
625643
self.method,
@@ -636,11 +654,11 @@ def make_api_request(
636654
)
637655
response.raise_for_status()
638656
self.logger.info(
639-
f"Request successful with status code {response.status_code}."
657+
"Request successful with status code %d.", response.status_code
640658
)
641659
return response.json()
642660
except requests.exceptions.RequestException as e:
643-
self.logger.error(f"API request failed: {e}")
661+
self.logger.error("API request failed: %s", e, exc_info=True)
644662
return None
645663

646664

0 commit comments

Comments
 (0)