66import json
77import linecache
88import os
9- from typing import TYPE_CHECKING , Optional
9+ from pathlib import Path
10+ from typing import Optional
1011
1112import dill as pickle
1213
1314from codeflash .code_utils .tabulate import tabulate
1415from codeflash .languages import is_python
1516
16- if TYPE_CHECKING :
17- from pathlib import Path
18-
1917
2018def show_func (
2119 filename : str , start_lineno : int , func_name : str , timings : list [tuple [int , int , float ]], unit : float
@@ -80,9 +78,7 @@ def show_text(stats: dict) -> str:
8078 return out_table
8179
8280
83- def show_text_non_python (
84- stats : dict , line_contents : dict [tuple [str , int ], str ]
85- ) -> str :
81+ def show_text_non_python (stats : dict , line_contents : dict [tuple [str , int ], str ]) -> str :
8682 """Show text for non-Python timings using profiler-provided line contents."""
8783 out_table = ""
8884 out_table += "# Timer unit: {:g} s\n " .format (stats ["unit" ])
@@ -100,13 +96,13 @@ def show_text_non_python(
10096 table_rows = []
10197 for lineno , nhits , time in timings :
10298 percent = "" if total_time == 0 else "%5.1f" % (100 * time / total_time )
103- time_disp = "% 5.1f" % time
99+ time_disp = f" { time : 5.1f} "
104100 if len (time_disp ) > default_column_sizes ["time" ]:
105- time_disp = "% 5.1g" % time
101+ time_disp = f" { time : 5.1g} "
106102 perhit = (float (time ) / nhits ) if nhits > 0 else 0.0
107- perhit_disp = "% 5.1f" % perhit
103+ perhit_disp = f" { perhit : 5.1f} "
108104 if len (perhit_disp ) > default_column_sizes ["perhit" ]:
109- perhit_disp = "% 5.1g" % perhit
105+ perhit_disp = f" { perhit : 5.1g} "
110106 nhits_disp = "%d" % nhits # noqa: UP031
111107 if len (nhits_disp ) > default_column_sizes ["hits" ]:
112108 nhits_disp = f"{ nhits :g} "
@@ -115,11 +111,7 @@ def show_text_non_python(
115111
116112 table_cols = ("Hits" , "Time" , "Per Hit" , "% Time" , "Line Contents" )
117113 out_table += tabulate (
118- headers = table_cols ,
119- tabular_data = table_rows ,
120- tablefmt = "pipe" ,
121- colglobalalign = None ,
122- preserve_whitespace = True ,
114+ headers = table_cols , tabular_data = table_rows , tablefmt = "pipe" , colglobalalign = None , preserve_whitespace = True
123115 )
124116 out_table += "\n "
125117 return out_table
@@ -159,17 +151,15 @@ def parse_line_profile_results(line_profiler_output_file: Optional[Path]) -> dic
159151 line_num = int (line_str )
160152 line_num = int (line_num )
161153
162- lines_by_file .setdefault (file_path , []).append (
163- (line_num , int (stats .get ("hits" , 0 )), int (stats .get ("time" , 0 )))
164- )
154+ lines_by_file .setdefault (file_path , []).append ((line_num , int (stats .get ("hits" , 0 )), int (stats .get ("time" , 0 ))))
165155 line_contents [(file_path , line_num )] = stats .get ("content" , "" )
166156
167157 for file_path , line_stats in lines_by_file .items ():
168158 sorted_line_stats = sorted (line_stats , key = lambda t : t [0 ])
169159 if not sorted_line_stats :
170160 continue
171161 start_lineno = sorted_line_stats [0 ][0 ]
172- grouped_timings [(file_path , start_lineno , os . path . basename (file_path ))] = sorted_line_stats
162+ grouped_timings [(file_path , start_lineno , Path (file_path ). name )] = sorted_line_stats
173163
174164 stats_dict ["timings" ] = grouped_timings
175165 stats_dict ["unit" ] = 1e-9
0 commit comments