@@ -16,6 +16,7 @@ def log_double_accounting(
1616 filtered_names : Dict [Tuple [str , ...], Set [str ]],
1717 exception_names : Dict [Tuple [str , ...], Set [str ]],
1818 export_path : Path ,
19+ debug : bool = False ,
1920):
2021 """Write filtered and exceptional activity names by category to Excel.
2122
@@ -25,9 +26,14 @@ def log_double_accounting(
2526 :type exception_names: dict[tuple[str, ...], set[str]]
2627 :param export_path: Destination Excel workbook path.
2728 :type export_path: pathlib.Path
29+ :param debug: If True, write to Excel. If False, skip Excel writing.
30+ :type debug: bool
2831 :returns: ``None``
2932 :rtype: None
3033 """
34+ # Only write to Excel if debug=True
35+ if not debug :
36+ return
3137
3238 data_filtered = {
3339 "/" .join (category ): list (names )
@@ -105,6 +111,7 @@ def log_double_accounting_flows(
105111 stats : Dict ,
106112 region : str ,
107113 export_path : Path ,
114+ debug : bool = False ,
108115):
109116 """Write detailed zeroed flows to an Excel sheet.
110117
@@ -118,6 +125,9 @@ def log_double_accounting_flows(
118125 :type region: str
119126 :param export_path: Destination Excel workbook path.
120127 :type export_path: pathlib.Path
128+ :param debug: If True, include detailed flow-by-flow breakdown. If False, only
129+ write summary and aggregated statistics (much faster for large datasets).
130+ :type debug: bool
121131 :returns: ``None``
122132 :rtype: None
123133 """
@@ -206,6 +216,18 @@ def log_double_accounting_flows(
206216
207217 sheet_name = f"Zeroed Flows - { region } "
208218
219+ # Only write to Excel if debug=True (to avoid performance hit on large runs)
220+ if not debug :
221+ logger .info (
222+ f"[Double Accounting - { region } ] "
223+ f"Zeroed: { total_zeroed } , "
224+ f"Kept internal: { kept_internal } , "
225+ f"Kept FUs: { kept_fus } , "
226+ f"Kept diagonal: { kept_diagonal } "
227+ f"(Excel logging skipped - set debug=True to write to Excel)"
228+ )
229+ return
230+
209231 try :
210232 # Determine if file exists and mode
211233 file_exists = export_path .exists ()
@@ -220,9 +242,6 @@ def log_double_accounting_flows(
220242 old_sheet = writer .book .worksheets [idx ]
221243 writer .book .remove (old_sheet )
222244 writer .book .create_sheet (sheet_name , idx )
223- print (f" → Replaced existing sheet '{ sheet_name } '" )
224- else :
225- print (f" → Creating new sheet '{ sheet_name } '" )
226245
227246 # Write summary (always first)
228247 summary_df .to_excel (writer , sheet_name = sheet_name , index = False , startrow = 0 )
@@ -250,14 +269,25 @@ def log_double_accounting_flows(
250269 )
251270 next_row += len (note_df ) + 2
252271
253- # Write detailed flows ONLY if not empty
254- if not flows_df .empty :
272+ # Write detailed flows ONLY if debug=True (can be very large, e.g., 50k+ rows)
273+ if not flows_df .empty and debug :
255274 flows_df .to_excel (
256275 writer ,
257276 sheet_name = sheet_name ,
258277 index = False ,
259278 startrow = next_row ,
260279 )
280+ elif not flows_df .empty :
281+ # Add a note that detailed flows are available with debug=True
282+ note_df = pd .DataFrame ({
283+ "Note" : [
284+ f"Detailed flows ({ len (flows_df ):,} rows) not written. "
285+ "Set debug=True to include detailed flow-by-flow breakdown."
286+ ]
287+ })
288+ note_df .to_excel (
289+ writer , sheet_name = sheet_name , index = False , startrow = next_row
290+ )
261291
262292 logger .info (
263293 f"Double accounting flows logged to { export_path } (sheet: { sheet_name } )"
0 commit comments